Understanding Reflection Tamir Khason Technology Consultor ¹ mPrest Systems http://blogs.microsoft.co.il/blogs/tamir Session Code: DVP307 ¹ con·sul·tor (kən sult ′ ər) - one of a group of priests appointed to advise and assist a bishop
CLR – compilation and execution Execution Compilation Source code Language compiler JIT compiler Before installation or the first time each method is called MSIL Metadata Code Native code
Reflection overview
All types in the CLR are self-describing
CLR provides a reader and writer for type definitions
System.Reflection & System.Reflection.emit
You can ‘read’ programs
You can map between type systems
You can interrogate objects and types inside a running program
What is “Reflection”?
“ Reflection provides objects that encapsulate assemblies, modules, and types. ” (MSDN)
“ You can use reflection to dynamically create an instance of a type... ” (MSDN)
“ You can then invoke the type's methods or access its fields and properties. ” (MSDN)
“ Reflection.Emit namespace contains classes that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) ” (MSDN)
Where is “Reflection”?
System.Reflection namespace contains most Reflection classes.
System.Type is Reflection class used to access type metadata.
… more…
How to “Reflection”?
Viewing metadata
Performing type discovery
Late binding to methods and properties (Dynamic Invocation)
Creating types at runtime (Reflection Emit)
How to… perform type discovery
You can use reflection to explore and examine the contents of an assembly programmatically
You can find
methods, fields, properties, and events associated with a type
Detail implementation information through flag-field
Can invoke method through Reflection
ConstructorInfo
Same features as MethodInfo, just for constructors
FieldInfo vs. PropertyInfo
FieldInfo
Field data type and attributes
Static or Instance field, protection level
Can set value through reflection
Provides low-level, direct access with SetValueDirect()
PropertyInfo
Property type and attributes
Test methods for readability, writeability
"get" and "set" MethodInfo
Indexer ParameterInfo
Can invoke "set" and "get" through Reflection
We can clone it… Type kidType = oldKid.GetType(); Kid newKid = new Kid(); foreach (var m in kidType.GetProperties()) { m.SetValue(newKid , m.GetValue(oldKid, null), null); } Unknown object GetType().GetMembers() For Each Member FieldInfo.Copy old value to new Object
Clone optimization Kid CloneKid(Kid) { newKid.Height = oldKid.Height newKid.Weight = oldKid.Weight } Unknown object GetType().GetMembers() For Each Member Create Code that copies members Save as Method for later Invocation
Clone optimized Unknown object Has custom method? Execute Yes Copy Members No
What is… attributes
An attribute is an object that represents data you want to associate with an element in your program.
Two Kinds of Attributes:
Intrinsic : Supplied as part of the Common Language Runtime (CLR)
Custom : Attributes you create for your own purposes
0 comments
Post a comment