Creating an Assembly Programmatically
page 2 of 7
by Uday Denduluri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 42295/ 104

Assembly creation

The code shown in Listing 1 is to create an assembly programmatically. The code written is written in the C# language. The purpose of the code is to create an assembly with a class added to it. We also add a method to the class and finally save the dynamic assembly to the local system.

Listing 1

AppDomain currentDomain = Thread.GetDomain();
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "Test";
myAssemblyName.Version = new Version(1, 0, 0, 0);
AssemblyBuilder builder = currentDomain.DefineDynamicAssembly(myAssemblyName,
 AssemblyBuilderAccess.RunAndSave);
ModuleBuilder ObjModuleBuilder = builder.DefineDynamicModule("MyModule");
ObjModuleBuilder.DefineType("ClassName",
 TypeAttributes.Class).DefineMethod("MethodName",
 MethodAttributes.Public, CallingConventions.Standard);
builder.Save("MyAssembly.dll");

The AssemblyBuilder class is completely responsible for the creation of an assembly. An instance of the AssemblyBuilder class can be created by the current AppDomain. The steps for creating the assembly are listed below.

Step 1: Get the current application domain from the current thread.

Step 2: Create an object of the type AssemblyBuilder from the current application domain.

Step 3: Create the module builder object from the AssemblyBuilder.

Step 4: Create the different types, methods, etc. and add them to the Module Builder.

Step 5: Save the builder to the local system.

The complete description of each and every class used is illustrated in the next section.


View Entire Article

User Comments

Title: no explantion   
Name: divys
Date: 2012-10-03 2:13:12 AM
Comment:
hi where is the coding part
Title: method and purpose for creating assemblies   
Name: alice
Date: 2010-07-26 8:18:31 AM
Comment:
can u elaborate more on my title i dnt understand what should be written pliz
Title: Nice   
Name: sandip
Date: 2010-07-13 5:32:38 AM
Comment:
Good, its good information
Title: Hum Tum   
Name: Nitin Tiwari
Date: 2007-09-22 4:49:08 AM
Comment:
very Nice description....fine






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-11-12 2:22:09 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search