ABHISHEK BISWAS.NET Reflection Dynamically Create, Find and Invoke Types
References Video: uq6Ur7Dchttps:// uq6Ur7Dc
.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 = 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);
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() ) { … };
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
Invoking Methods Dynamic Invocation through Reflection Support for late binding MethodInfo.Invoke() FieldInfo.SetValue() PropertyInfo.SetValue()
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)
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)
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)
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")
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 potsdam.de/teaching/componentVl05/slides/Net_V L2_02_Reflection.pdf potsdam.de/teaching/componentVl05/slides/Net_V L2_02_Reflection.pdf us/library/system.reflection.emit.assemblybuilder% 28v=VS.100%29.asp us/library/system.reflection.emit.assemblybuilder% 28v=VS.100%29.asp