C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Building .NET Assemblies Dynamically


Posted Date: 15 Jun 2005    Resource Type: Articles    Category: .NET Framework
Author: gaurav trivediMember Level: Bronze    
Rating: 1 out of 5Points: 7



Building .NET Assemblies Dynamically

by: Gaurav Trivedi

Introduction:
These article as written just for help hope this will help you.

The .NET Framework allows an assembly to inspect and manipulate itself at runtime through System.Runtime.Reflection namespace and associated classes.

Using the .NET Reflection classes you can check the methods in a class, invoke them, passing parameters. .NET Reflection opens a way to build the classes and assemblies and compile and run them dynamically at runtime using System.CodeDom.Compiler namespace and associated classes.

There are some application types that require to generate source code in multiple languages at runtime. The .NET Framework provides a feature called Code Document Object Model (CodeDOM) that enables the output of source code in multiple programming languages at run time, based on a single model that represents the code to render.

You just wonder why somebody wants to generate assemblies dynamically at runtime and execute. Take ASP.NET for example, it uses the CodeDOM to create object graphs that it compiles into assemblies that can render HTML pages and controls.

The following example shows you how to generate a simple class, build an assembly using System.CodeDom.Compiler namespace and the associated classes and finally invoke a method on it by executing the assembly. Once it is compiled, .NET Reflection allows you to execute the class and invoke methods on it.

Before running this example, please ensure that you have .NET Framework installed your system, and copy the code below into a file named DynamicAssembly.cs and from the command line compile DynamicAssembly.cs using the C# compiler.
csc /t:exe DynamicAssembly.cs

This builds an executable called DynamicAssembly.exe. Once the Executable is built run it from the command line. This first builds a C# file called CSharp.cs file and then builds CSharp.dll assembly. Then .NET Reflection loads the assembly and invokes the method HelloWorld(), which outputs the text string "Hello World - CSharp.Com Rocks" on the console.
using System;using System.IO;using Microsoft.CSharp; //We are building a CSharp classusing System.CodeDom.Compiler;using System.Reflection;namespace DynamicAssembly{ class CreateCompileExecute { [STAThread] static void Main(string[] args) { // Creates a text file to store the new class StreamWriter streamWriter = File.CreateText("CSharp.cs"); streamWriter.WriteLine ("using System;"); streamWriter.WriteLine ("namespace CSharpRocks"); streamWriter.WriteLine ("{"); streamWriter.WriteLine ("class CSharp"); streamWriter.WriteLine ("{"); streamWriter.WriteLine ("public CSharp() { Console.WriteLine(\"The CSharp type is constructed\"); }"); streamWriter.WriteLine ("public void HelloWorld() { Console.WriteLine(\"Hello World - CSharp.Com Rocks.\"); }"); streamWriter.WriteLine ("}"); streamWriter.WriteLine ("}"); streamWriter.Close(); // Create the C# compiler CSharpCodeProvider csCompiler = new CSharpCodeProvider(); ICodeCompiler iCodeCompiler = csCompiler.CreateCompiler(); // input params for the compiler CompilerParameters compilerParams = new CompilerParameters(); compilerParams.OutputAssembly = "CSharp.dll"; compilerParams.ReferencedAssemblies.Add("system.dll"); // generate the DLL compilerParams.GenerateExecutable = false; // Run the compiler and build the assembly iCodeCompiler.CompileAssemblyFromFile(compilerParams, "CSharp.cs"); // Load the generated assembly into the ApplicationDomain Assembly asm = Assembly.LoadFrom("CSharp.dll"); Type t = asm.GetType("CSharpRocks.CSharp"); // BindingFlags enumeration specifies flags that control binding and // the way in which the search for members and types is conducted by reflection. // The following specifies the Access Control of the bound type BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; // Construct an instance of the type and invoke the member method Object obj = t.InvokeMember("HelloWorld", bflags | BindingFlags.CreateInstance, null, null, null); // Call the method t.InvokeMember("HelloWorld", bflags | BindingFlags.InvokeMethod, null, obj, null); } }}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Cleaningup something using GarbageCollector
Previous Resource: Custom ASP.NET Datagrid Paging With Exact Count
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use