NET Reflection Dynamically Create Find and Invoke Types




![Reflection at Work Assembly assm = app. Domain. Load(buffer); Type[] types = assm. Get. Reflection at Work Assembly assm = app. Domain. Load(buffer); Type[] types = assm. Get.](https://slidetodoc.com/presentation_image_h2/bc006564c5138834fb1ff516c863c8f1/image-5.jpg)









- Slides: 14
. NET Reflection Dynamically Create, Find and Invoke Types ABHISHEK BISWAS
References �Video: https: //www. youtube. com/watch? v=y 8 - uq 6 Ur 7 Dc �https: //www. youtube. com/watch? v=-w. Kqw. AJ-ipg
. NET Reflection �Features Create and Extend types, modules & assemblies at runtime Read type metadata at runtime Invoke type methods at runtime Language Independent �Advantages Single location for type information and code Code is contained within type information Every. NET object can be queried for its type
Metadata Components
Reflection at Work Assembly assm = app. Domain. Load(buffer); Type[] types = assm. Get. Types(); foreach (Type type in types) {Console. Write. Line(type. Full. Name); } Method. Info my. Method = assm. Get. Type("HW 3. hash. Key"). Get. Method("Gen. Hash"); object obj = Activator. Create. Instance (assm. Get. Type("HW 3. hash. Key")); my. Method. Invoke(obj, null);
Get. Type() Function Breakdown � Member of System. Object Parent of all. NET classes Available on every. NET class & simple type � Returns System. Type object � Type Identity Types have unique identity across any assembly Types can be compared for identity • if ( a. Get. Type() == b. Get. Type() ) { … };
System. Type � Access to meta-data for any. NET type � Allows drilling down into all facets of a type � Category: Simple, Enum, Struct or Class Is. Value. Type, Is. Interface, Is. Class Is. Not. Public, Is. Sealed Is. Abstract � Methods and Constructors, Parameters and Return Member. Info: Get. Members(), Find. Members() Field. Info: Get. Fields(), Property. Info: Get. Properties() Get. Constructors(), Get. Methods(), Get. Events() � Fields and Properties, Arguments and Attributes � Events, Delegates and Namespaces
Invoking Methods � Dynamic Invocation through Reflection � Support for late binding Method. Info. Invoke() Field. Info. Set. Value() Property. Info. Set. Value()
Creating a New Type/Module/Assembly �Namespace “System. Reflection. Emit” Dim a. Name As New Assembly. Name("Dynamic. Assembly. Example") Dim ab As Assembly. Builder = App. Domain. Current. Domain. Define. Dynamic. Assembly(a. Name, Assembly. Builder. Access. Run. And. Save) Dim mb As Module. Builder = ab. Define. Dynamic. Module(a. Name, a. Name & ". dll") Dim tb As Type. Builder = mb. Define. Type("My. Dynamic. Type", Type. Attributes. Public)
Adding Field and Constructor Dim fb. Number As Field. Builder = tb. Define. Field("m_number“, Get. Type(Integer), Field. Attributes. Private) Dim parameter. Types() As Type = { Get. Type(Integer) } Dim ctor 1 As Constructor. Builder = tb. Define. Constructor( Method. Attributes. Public, Calling. Conventions. Standard, parameter. Types) Dim ctor 1 IL As ILGenerator = ctor 1. Get. ILGenerator() ctor 1 IL. Emit(Op. Codes. Ldarg_0) ctor 1 IL. Emit(Op. Codes. Call, Get. Type(Object). Get. Constructor(Type. Em pty. Types)) ctor 1 IL. Emit(Op. Codes. Ldarg_0) ctor 1 IL. Emit(Op. Codes. Ldarg_1) ctor 1 IL. Emit(Op. Codes. Stfld, fb. Number) ctor 1 IL. Emit(Op. Codes. Ret)
Adding Method Dim meth As Method. Builder = tb. Define. Method("My. Method", Method. Attributes. Public, Get. Type(Integer), New Type(){Get. Type(Integer)}) Dim meth. IL As ILGenerator = meth. Get. ILGenerator() meth. IL. Emit(Op. Codes. Ldarg_0) meth. IL. Emit(Op. Codes. Ldfld, fb. Number) meth. IL. Emit(Op. Codes. Ldarg_1) meth. IL. Emit(Op. Codes. Mul) meth. IL. Emit(Op. Codes. Ret)
Finishing Dim t As Type = tb. Create. Type() ab. Save(a. Name & ". dll") Dim mi As Method. Info = t. Get. Method("My. Method") Dim pi As Property. Info = t. Get. Property("Number")
Why? �Build classes dynamically from script-like code ASP. NET, Regular Expressions do just that ! � Generate code from visual development tools e. g. build interfaces, base classes from UML �Create dynamic wrappers for existing code �Transfer code-chunks to remote machines Distributed processing scenarios
References �http: //www. dcl. hpi. uni- potsdam. de/teaching/component. Vl 05/slides/Net_V L 2_02_Reflection. pdf �http: //msdn. microsoft. com/enus/library/system. reflection. emit. assemblybuilder% 28 v=VS. 100%29. asp