Presentation is loading. Please wait.

Presentation is loading. Please wait.

ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types.

Similar presentations


Presentation on theme: "ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types."— Presentation transcript:

1 ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types

2 References Video: https://www.youtube.com/watch?v=y8- uq6Ur7Dchttps://www.youtube.com/watch?v=y8- uq6Ur7Dc https://www.youtube.com/watch?v=-wKqwAJ-ipg

3 .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

4 Metadata Components

5 Reflection at Work Assembly assm = appDomain.Load(buffer); Type[] types = assm.GetTypes(); foreach (Type type in types) {Console.WriteLine(type.FullName); } MethodInfo myMethod = assm.GetType("HW3.hashKey"). GetMethod("GenHash"); object obj = Activator.CreateInstance (assm.GetType("HW3.hashKey")); myMethod.Invoke(obj, null);

6 GetType() 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.GetType() == b.GetType() ) { … };

7 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  IsValueType, IsInterface, IsClass  IsNotPublic, IsSealed  IsAbstract Methods and Constructors, Parameters and Return  MemberInfo: GetMembers(), FindMembers()  FieldInfo: GetFields(), PropertyInfo: GetProperties()  GetConstructors(), GetMethods(), GetEvents() Fields and Properties, Arguments and Attributes Events, Delegates and Namespaces

8 Invoking Methods Dynamic Invocation through Reflection Support for late binding  MethodInfo.Invoke()  FieldInfo.SetValue()  PropertyInfo.SetValue()

9 Creating a New Type/Module/Assembly Namespace “System.Reflection.Emit” Dim aName As New AssemblyName("DynamicAssemblyExample") Dim ab As AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave) Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name, aName.Name & ".dll") Dim tb As TypeBuilder = mb.DefineType("MyDynamicType", TypeAttributes.Public)

10 Adding Field and Constructor Dim fbNumber As FieldBuilder = tb.DefineField("m_number“, GetType(Integer), FieldAttributes.Private) Dim parameterTypes() As Type = { GetType(Integer) } Dim ctor1 As ConstructorBuilder = tb.DefineConstructor( MethodAttributes.Public, CallingConventions.Standard, parameterTypes) Dim ctor1IL As ILGenerator = ctor1.GetILGenerator() ctor1IL.Emit(OpCodes.Ldarg_0) ctor1IL.Emit(OpCodes.Call,GetType(Object).GetConstructor(Type.E mptyTypes)) ctor1IL.Emit(OpCodes.Ldarg_0) ctor1IL.Emit(OpCodes.Ldarg_1) ctor1IL.Emit(OpCodes.Stfld, fbNumber) ctor1IL.Emit(OpCodes.Ret)

11 Adding Method Dim meth As MethodBuilder = tb.DefineMethod("MyMethod", MethodAttributes.Public, GetType(Integer), New Type(){GetType(Integer)}) Dim methIL As ILGenerator = meth.GetILGenerator() methIL.Emit(OpCodes.Ldarg_0) methIL.Emit(OpCodes.Ldfld, fbNumber) methIL.Emit(OpCodes.Ldarg_1) methIL.Emit(OpCodes.Mul) methIL.Emit(OpCodes.Ret)

12 Finishing Dim t As Type = tb.CreateType() ab.Save(aName.Name & ".dll") Dim mi As MethodInfo = t.GetMethod("MyMethod") Dim pi As PropertyInfo = t.GetProperty("Number")

13 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

14 References http://www.dcl.hpi.uni- potsdam.de/teaching/componentVl05/slides/Net_V L2_02_Reflection.pdf http://www.dcl.hpi.uni- potsdam.de/teaching/componentVl05/slides/Net_V L2_02_Reflection.pdf http://msdn.microsoft.com/en- us/library/system.reflection.emit.assemblybuilder% 28v=VS.100%29.asp http://msdn.microsoft.com/en- us/library/system.reflection.emit.assemblybuilder% 28v=VS.100%29.asp


Download ppt "ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types."

Similar presentations


Ads by Google