I want to create dynamic type with refelction emit like: public class ObservableTestColleciton<T> : ObservableCollection<T> { public T Parent { get; set; } public ObservableTestColleciton(T parent) { Parent = parent; } public ObservableTestColleciton(T parent, IEnumerable<T> source):base(source) { Parent = parent; } }
The code I could not complete is this like:
' AppDomain myDomain = AppDomain.CurrentDomain; AssemblyName myAsmName = new AssemblyName("AAB"); AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(myAsmName,AssemblyBuilderAccess.Save); ModuleBuilder myModule = myAssembly.DefineDynamicModule(myAsmName.Name,myAsmName.Name + ".dll"); TypeBuilder myType = myModule.DefineType("ObservableTestCollection", TypeAttributes.Class | TypeAttributes.Public);
string[] typeParamNames = { "T" };
GenericTypeParameterBuilder[] typeParams = myType.DefineGenericParameters(typeParamNames);
Type observableOf = typeof(ObservableCollection<>);
Type genOb = observableOf.MakeGenericType(typeParams[0]);
FieldBuilder myField = myType.DefineField("Parent", typeParams[0], FieldAttributes.Public);
ConstructorBuilder constructor = myType.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, Type.EmptyTypes);
var type = myType.CreateType();
var obj = Activator.CreateInstance(type);
myAssembly.Save("AAB.dll");'
Aucun commentaire:
Enregistrer un commentaire