Title: Reflection in .Net
1Reflection in .Net
2Objective
- Explain .NET Reflection
- When to use it?
- How to use it?
- Topic is in the final exam
3Overview
- 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
4What is Reflection
- A way to acquire important information at run
time about the assemblies it loads.
5Uses 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
VB
Cobol
C
C
.NET languages
Language compilers
CIL code ( metadata)
Loader/verifier
JIT compiler
Uncompiled method call
Managed Code
Execution
7Metadata
- 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
8Uses of Metadata
- Dynamic Type System
- Allows on-the-fly creation of assemblies
- .NET Compilers use .NET to emit .NET code
9MetaData Type Info at Runtime
serializablepublic 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)
FirstNameFirstLastNameLast public
bool Save() System.Type t
this.GetType() foreach( FieldInfo f in
t.GetFields() ) ...
System.Type
Attributes
Events
Fields
Properties
Constructors
Parameters
Methods
10Viewing metadata
- Add custom attributes to a compiled executables
metadata - Why/When to use this?
11Scenario
- 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.Cons
tructor - AttributeTargets.Fiel
d - AttributeTargets.Meth
od - AttributeTargets.Prop
erty, - 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) -
-
14Demo1
- demo1.doc
- Project Programming_Csharp
15Performing type discovery
- This allows you to examine the types in an
assembly. - Project Programming_Csharp2
16Reflecting 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.d
ll") - Type types a.GetTypes()
- foreach (Type t in types)
-
- Console.WriteLine("Type is 0",
t) -
- Console.WriteLine(
- "0 types found", types.Length)
-
-
17Reflecting 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) -
18Reflecting 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)
-
-
19Finding 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)
-
-
20Late 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).
21Creating 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.
22Static Method
- Wrote before runtime (before compilation)
- public int DoSumLooping(int initialVal)
-
- int result 0
- for(int i 1i ltinitialVali)
-
- result i
-
- return result
-
- Summation of N 123456..N
23Dynamic 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 lt theValuei)
-
- generator.Emit(OpCodes.Ldc_I4, i)
- generator.Emit(OpCodes.Add)
-
-
24Demo
25Problems
- Reflection APIs are known to cause problem on
obfuscated assemblies.
26References
- http//www.oreilly.com/catalog/progcsharp/chapter/
ch18.html - http//en.csharp-online.net/Introduction_to_CLIE2
8094Execution_and_Deployment_Models - http//www.dcl.hpi.uni-potsdam.de/teaching/compone
ntVl05/slides/Net_VL2_02_Reflection.pdf
27Reference
- medialab.di.unipi.it/web/AP/Reflection.ppt
- Programming Microsoft .NET by Jeff Prosise
28 29