dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Reflection Part II: Emit : Page 3

In our previous article, Reflection Part 1: Discovery and Execution , we introduced the System.Reflection namespace and its classes which allow developers to view assembly metadata, query for and discover types, and invoke code—all at run-time. In this article we will examine reflection emit—the ability to dynamically generate code at runtime.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Namespaces and Classes
As you've already seen, the Reflection.Emit namespace contains a core set of "builder" classes that you use to create types and the various attributes, methods, fields, properties, and so on that are associated with the new type. Table 1 describes the primary classes you'll use when emitting code with reflection.

Table 1: Reflection emit-related class reference.

Namespace.Class

System.Reflection.Emit.AssemblyBuilder



Primary Use

Defines a dynamic .NET assembly: A self-describing .NET building block. A dynamic assembly is one that was specifically generated through reflection emit. This class inherits from System.Reflection.Assembly.

Example Call

Dim ab As AssemblyBuilder

Dim ad As AppDomain

ad = Thread.GetDomain()

ab = ad.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run)

Namespace.Class

System.Reflection.Emit.ConstructorBuilder

Primary Use

Used to create and represent the constructor of a dynamic class. It encapsulates all information related to the constructor including its name, function signature, and code body. This is only needed if you are creating a parameter-driven constructor or you're overriding the default behavior of the parent class's constructor.

Example Call

Dim ourClass As TypeBuilder = [module].DefineType("ourClass", _

TypeAttributes.Public)

 

Dim ctorArgs As Type() = {GetType(String)}

Dim ctor As ConstructorBuilder = _

ourClass.DefineConstructor(MethodAttributes.Public, _

CallingConventions.Standard, constructorArgs)

Namespace.Class

System.Reflection.Emit.CustomAttributeBuilder

Primary Use

Used to create custom attributes for dynamic classes.

Namespace.Class

System.Reflection.Emit.EnumBuilder

Primary Use

Defines and represents an enumeration.

Namespace.Class

System.Reflection.Emit.EventBuilder

Primary Use

EventBuilder creates events for a dynamic class.

Namespace.Class

System.Reflection.Emit.FieldBuilder

Primary Use

Use FieldBuilder to define fields on a class.

Namespace.Class

System.Reflection.Emit.ILGenerator

Primary Use

Used to generate MSIL code (Microsoft Intermediate Language).

Example Call

Dim gen As ILGenerator = someMethod.GetILGenerator()

gen.Emit(OpCodes.Ldarg_0)

gen.Emit(OpCodes.Ret)

Namespace.Class

System.Reflection.Emit.LocalBuilder

Primary Use

Creates variables that are local to a given method or constructor.

Namespace.Class

System.Reflection.Emit.MethodBuilder

Primary Use

Used to create and represent methods inside of a class.

Example Call

Dim t as Type = myModule.GetType("MyClassName")

Namespace.Class

System.Reflection.Emit.MethodRental

Primary Use

A utility class used to swap a method into a dynamically created class from some other class template. This is useful when you need to quickly re-create functionality via a method that already exists elsewhere.

Namespace.Class

System.Reflection.Emit.ParameterBuilder

Primary Use

Creates parameters for use in a method signature.

Namespace.Class

System.Reflection.Emit.PropertyBuilder

Primary Use

Defines properties for a type.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date