Tip of the day : Create dynamically an assembly in the current domain

By Bechir Bejaoui Jul 05, 2008
Create dynamically an assembly in the current domain

if you want to create an assembly dynamically in the current domain, this is a method to perform this:

using System;

using System.Reflection;

using System.Reflection.Emit;

namespace yourProjectname

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                //Define an assembly name

                AssemblyName oAssemblyName = new AssemblyName("myAssembly");

                //Build the assembly

                AssemblyBuilder oAssBuilder =

                    AppDomain.CurrentDomain.DefineDynamicAssembly(oAssemblyName, AssemblyBuilderAccess.Save);

                //Save the assembly as ILOnly working on I386 32 bit processor

                //An argument exception will be thrown if you precise the path

                // C:\...

                oAssBuilder.Save(@"myAssembly.dll");

                Console.WriteLine("The assembly is dynamically created in the current domain");

                Console.Read();

            }

            catch (AppDomainUnloadedException caught)

            {

                Console.WriteLine(caught.Message);

            }

        }

    }

}

It is also possible to create assemblies in a new domain you just replace currentDomain by createDomain

Bechir Bejaoui

The author holds a master degree in NTIC specialized  in software developement delivered by the high school of communication SUPCOM, he also holds a bachelor degree in finance delivered by  the  economic s... Read more

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs