Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam.

Similar presentations


Presentation on theme: "Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam."— Presentation transcript:

1 Reflection in.Net Siun-Wai Seow

2 Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam

3 Overview Reflection.Net Execution Model Show how applications can use Reflection to explore type information and how they can build types at runtime Demo for each sub-topic

4 What is Reflection A way to acquire important information at run time about the assemblies it loads.

5 Uses of Reflection Viewing metadata (Attributes) Performing type discovery Late binding to methods and properties Dynamic Loading/Creating types at runtime (Reflection Emit) How is it done?

6 .Net Execution Model Cobol VB C++ C# CIL code (+ metadata) Loader/verifier Managed Code Uncompiled method call Execution Language compilers.NET languages JIT compiler

7 Metadata –Single location for type information and code –Types' metadata can be explored with Reflection –Code is literally contained within type information –Every.NET object can be queried for its type

8 Uses of Metadata Dynamic Type System –Allows on-the-fly creation of assemblies –.NET Compilers use.NET to emit.NET code

9 MetaData: Type Info at Runtime [serializable] public class Person : { public event OnSaveChange onsv; public Date DOB; public string FirstName; public string LastName; public string Name { get { return FirstName + " " + LastName; } } public Person(string First,string Last) { FirstName=First;LastName=Last; } public bool Save() { System.Type t = this.GetType() ; foreach( FieldInfo f in t.GetFields() ) {... } } System.Type Attributes Fields Properties Constructors Methods Events Parameters

10 Viewing metadata Add custom attributes to a compiled executable’s metadata Why/When to use this?

11 Scenario Suppose that your organization wants to keep track of bug fixes. You already keep a database of all your bugs, but you'd like to tie your bug reports to the fixes in the code. How do implement this?

12 [BugFixAttribute(121, "Jesse Liberty", "01/03/05")] [BugFixAttribute(107, "Jesse Liberty", "01/04/05", Comment = "Fixed off by one errors")] public class MyMath { …………………… } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)] public class BugFixAttribute : System.Attribute { ………………… }

13 public static void Main() { ……………….. object[] attributes; attributes = inf.GetCustomAttributes(typeof(BugFixAttribute), false); // iterate through the attributes, retrieving the // properties foreach (Object attribute in attributes) { BugFixAttribute bfa = (BugFixAttribute)attribute; Console.WriteLine("\nBugID: {0}", bfa.BugID); Console.WriteLine("Programmer: {0}", bfa.Programmer); Console.WriteLine("Date: {0}", bfa.Date); Console.WriteLine("Comment: {0}", bfa.Comment); }

14 Demo1 demo1.doc Project Programming_Csharp

15 Performing type discovery This allows you to examine the types in an assembly. Project Programming_Csharp2

16 Reflecting on an assembly using System; using System.Reflection; public class Tester { //Reflecting on an assembly public static void Main() { // what is in the assembly Assembly a = Assembly.Load("Mscorlib.dll"); Type[] types = a.GetTypes(); foreach (Type t in types) { Console.WriteLine("Type is {0}", t); } Console.WriteLine( "{0} types found", types.Length); }

17 Reflecting on a Type public static void Main() { // examine a single object Type theType = Type.GetType( "System.Reflection.Assembly"); Console.WriteLine( "\nSingle Type is {0}\n", theType); }

18 Reflecting on the members of a type public static void Main() { // examine a single object Type theType = Type.GetType( "System.Reflection.Assembly"); Console.WriteLine( "\nSingle Type is {0}\n", theType); // get all the members MemberInfo[] mbrInfoArray = theType.GetMembers(); foreach (MemberInfo mbrInfo in mbrInfoArray) { Console.WriteLine("{0} is a {1}", mbrInfo, mbrInfo.MemberType); }

19 Finding type methods public static void Main() { // examine a single object Type theType = Type.GetType( "System.Reflection.Assembly"); Console.WriteLine( "\nSingle Type is {0}\n", theType); MemberInfo[] mbrInfoArray = theType.GetMethods(); foreach (MemberInfo mbrInfo in mbrInfoArray) { Console.WriteLine("{0} is a {1}", mbrInfo, mbrInfo.MemberType); }

20 Late binding to methods and properties Performing late binding by dynamically instantiating and invoking methods on types. Done in homework 3 (application domain/stack walk/permission).

21 Creating types at runtime The ultimate use of reflection is to create new objects/types at runtime and then to use those objects to perform tasks. You might do this when a new class, created at runtime, will run significantly faster than more generic code created at compile time.

22 Static Method Wrote before runtime (before compilation) public int DoSumLooping(int initialVal) { int result = 0; for(int i = 1;i <=initialVal;i++) { result += i; } return result; } Summation of N : 1+2+3+4+5+6+…..+N

23 Dynamic Method created at run time // Push zero onto the stack. For each 'i' // less than 'theValue', // push 'i' onto the stack as a constant // add the two values at the top of the stack. // The sum is left on the stack. generator.Emit(OpCodes.Ldc_I4, 0); for (int i = 1; i <= theValue;i++) { generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Add); }

24 Demo Programming_CSharp3

25 Problems Reflection APIs are known to cause problem on obfuscated assemblies.

26 References http://www.oreilly.com/catalog/progcsharp/ chapter/ch18.htmlhttp://www.oreilly.com/catalog/progcsharp/ chapter/ch18.html http://en.csharp- online.net/Introduction_to_CLI%E2%80%9 4Execution_and_Deployment_Modelshttp://en.csharp- online.net/Introduction_to_CLI%E2%80%9 4Execution_and_Deployment_Models http://www.dcl.hpi.uni- potsdam.de/teaching/componentVl05/slide s/Net_VL2_02_Reflection.pdfhttp://www.dcl.hpi.uni- potsdam.de/teaching/componentVl05/slide s/Net_VL2_02_Reflection.pdf

27 Reference medialab.di.unipi.it/web/AP/Reflection.ppt Programming Microsoft.NET by Jeff Prosise

28 Questions

29 Thank you


Download ppt "Reflection in.Net Siun-Wai Seow. Objective Explain.NET Reflection When to use it? How to use it? Topic is in the final exam."

Similar presentations


Ads by Google