Home All Groups Group Topic Archive Search About

How to get the same assembly using ICodeCompiler or AssemblyBuilder

Author
28 Sep 2006 8:08 AM
ginnone
Hi.
I need to create an assembly dinamically, using AssemblyBuilder etc.
The resulting assembly must be te same I would get if I compiled such a
..cs file.
I don't want to use ICodeCompiler as I don't have the .cs file, but I
want to create an assembly as the one I would get if I could compile
such a file.

using System;
using System.Collections;
namespace COPERGMPS.HPSContentManagerLib.IICE.ItemClasses
{
    [Attribute1(Name="prova1" , Ver="1.0.0.0")]
    public class Class1 : BaseType
    {
        private String _prVal1;
        [Property1(Name="Val1", Label="Class1/Val1")]
        public String Val1
        {
            get { return _Val1; }
            set { _Val1 = value; SetValueChanged("Val1");}
        }
        public Class1()
        {
            _hashIsValueChanged["MUT_AZIENDA"] = false;
        }
    }
}

Could anyone help me? I already managed to do create the type, the
attributes, the properties, but I'm still missing something: how can I
reproduce the code "SetValueChanged" and "hashIsValueChanged" which
belong to my parent class?
This is what I have done yet:

AppDomain appDomain = Thread.GetDomain();
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "Class1";
AssemblyBuilder ab =
appDomain.DefineDynamicAssembly(assemblyName,AssemblyBuilderAccess.RunAndSave);
ModuleBuilder mb = ab.DefineDynamicModule(Class1);
TypeBuilder tb = mb.DefineType(ItemClassNamespace+"."+Class1,
TypeAttributes.Public);

ConstructorInfo ci =
typeof(SerializableAttribute).GetConstructor(ctorParams);
CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, new
object[] { });
tb.SetCustomAttribute(cab);

COPERGMPS.HPSContentManagerLib.IICE.IICEItemClassAttribute();
Type[] ctorParams = Type.EmptyTypes;
Type tt = typeof(Attribute1);
PropertyInfo[] properties = tt.GetProperties();
ConstructorInfo ci = typeof(Attribute1).GetConstructor(ctorParams);
CustomAttributeBuilder cab = new CustomAttributeBuilder(ci,new object
[] { } , properties , new object[] { "prova1", "1.0.0.0" } );
tb.SetCustomAttribute(cab);

tb.SetParent( typeof (BaseType) );

Type tipo = typeof(System.String);
FieldBuilder privateVar =
tb.DefineField("_prVal1",tipo,FieldAttributes.Private);
PropertyBuilder publicVar =
tb.DefineProperty("Val1",System.Reflection.PropertyAttributes.HasDefault,tipo,null);

Type[] ctorParams1 = Type.EmptyTypes;
Type tt1 = typeof(Property1);
PropertyInfo[] properties1 = tt1.GetProperties();
ConstructorInfo ci1 = typeof(Property1).GetConstructor(ctorParams1);
CustomAttributeBuilder cab1 = new CustomAttributeBuilder(ci1,new object
[] { } , properties1 , new object[] { "Val1", "Class1/Val1" } );
tb.SetCustomAttribute(cab1);

publicVar.SetCustomAttribute(cab1);

MethodAttributes getSetAttr = MethodAttributes.Public |
MethodAttributes.SpecialName | MethodAttributes.HideBySig;

MethodBuilder mbGet = tb.DefineMethod("get_Val1", getSetAttr,
typeof(string), Type.EmptyTypes);

ILGenerator ilGeneratorGet = mbGet.GetILGenerator();

ilGeneratorGet.Emit(OpCodes.Ldarg_0);
ilGeneratorGet.Emit(OpCodes.Ldfld, privateVar);
ilGeneratorGet.Emit(OpCodes.Ret);

MethodBuilder mbSet = tb.DefineMethod("set_Val1" getSetAttr, null, new
Type[] { typeof(string) });

ILGenerator ilGeneratorSet = mbSet.GetILGenerator();

ilGeneratorSet.Emit(OpCodes.Ldarg_0);
ilGeneratorSet.Emit(OpCodes.Ldarg_1);
ilGeneratorSet.Emit(OpCodes.Stfld, privateVar);
ilGeneratorSet.Emit(OpCodes.Ret);

publicVar.SetGetMethod(mbGet);
publicVar.SetSetMethod(mbSet);

ab.Save("provaDynamic.dll");

AddThis Social Bookmark Button