net reflection dynamically create, find and invoke types

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

Upload: pepper

Post on 07-Jan-2016

17 views

Category:

Documents


0 download

DESCRIPTION

.NET Reflection Dynamically Create, Find and Invoke Types. Abhishek Biswas. References. Video: https:// www.youtube.com/watch?v=y8-uq6Ur7Dc https://www.youtube.com/watch?v=-wKqwAJ-ipg. .NET Reflection. Features Create and Extend types, modules & assemblies at runtime - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: NET Reflection Dynamically Create, Find and Invoke Types

ABHISHEK BISWAS

.NET ReflectionDynamically Create, Find and

Invoke Types

Page 2: NET Reflection Dynamically Create, Find and Invoke Types

References

Video: https://www.youtube.com/watch?v=y8-uq6Ur7Dc

https://www.youtube.com/watch?v=-wKqwAJ-ipg

Page 3: NET Reflection Dynamically Create, Find and Invoke Types

.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

Page 5: NET Reflection Dynamically Create, Find and Invoke Types

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);

Page 6: NET Reflection Dynamically Create, Find and Invoke Types

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() ) { … };

Page 7: NET Reflection Dynamically Create, Find and Invoke Types

System.Type

Access to meta-data for any .NET typeAllows drilling down into all facets of a typeCategory: 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 AttributesEvents, Delegates and Namespaces

Page 8: NET Reflection Dynamically Create, Find and Invoke Types

Invoking Methods

Dynamic Invocation through Reflection

Support for late binding

MethodInfo.Invoke()

FieldInfo.SetValue()

PropertyInfo.SetValue()

Page 9: NET Reflection Dynamically Create, Find and Invoke Types

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)

Page 10: NET Reflection Dynamically Create, Find and Invoke Types

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

ypes)) ctor1IL.Emit(OpCodes.Ldarg_0) ctor1IL.Emit(OpCodes.Ldarg_1)ctor1IL.Emit(OpCodes.Stfld, fbNumber) ctor1IL.Emit(OpCodes.Ret)

Page 11: NET Reflection Dynamically Create, Find and Invoke Types

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)

Page 12: NET Reflection Dynamically Create, Find and Invoke Types

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")

Page 13: NET Reflection Dynamically Create, Find and Invoke Types

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 codeTransfer code-chunks to remote machines

Distributed processing scenarios