I have dynamically created the type "FruitTypes" with these two properties
private string _apple;
public string Apple
{
get { return _apple; }
set { _apple= value; }
}
private string _pear;
public string Pear
{
get { return _pear; }
set { _pear= value; }
}
Now the second type called "Farm" shall have two properties like this:
private string _ID;
public string ID
{
get { return _ID; }
set { _ID= value; }
}
private ObservableCollection<FruitTypes> _fruits;
public ObservableCollection<FruitTypes> Fruits
{
get { return _fruits; }
set { _fruits= value; }
}
I have no idea how to create the Farm. Can someone please help with with code samples? Many Thanks,
UPDATE: I create the fruitTypes like this:
TypeBuilder typeBldr = modBldr.DefineType("FruitTypes", TypeAttributes.Public | TypeAttributes.Class);
FieldBuilder field = typeBldr.DefineField("_apple", typeof(string), FieldAttributes.Private);
PropertyBuilder propertyBuilder = typeBldr.DefineProperty("Apple", PropertyAttributes.None, typeof(string), new[] { typeof(string) });
MethodAttributes GetSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;
MethodBuilder currGetPropMthdBldr = typeBldr.DefineMethod("get_Apple", GetSetAttr, typeof(string), Type.EmptyTypes);
ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
currGetIL.Emit(OpCodes.Ldarg_0);
currGetIL.Emit(OpCodes.Ldfld, field);
currGetIL.Emit(OpCodes.Ret);
MethodBuilder currSetPropMthdBldr = typeBldr.DefineMethod("set_Apple", GetSetAttr, null, new[] { typeof(string) });
ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
currSetIL.Emit(OpCodes.Ldarg_0);
currSetIL.Emit(OpCodes.Ldarg_1);
currSetIL.Emit(OpCodes.Stfld, field);
currSetIL.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(currGetPropMthdBldr);
propertyBuilder.SetSetMethod(currSetPropMthdBldr);
I do the same thing for a Pear Property within the same type.
Now how to wire them up is like this:
var tempName = new AssemblyName {Name = "MyTempAssembly"};
AssemblyBuilder assemBldr = AppDomain.CurrentDomain.DefineDynamicAssembly(tempName, AssemblyBuilderAccess.Run);
ModuleBuilder modBldr = assemBldr.DefineDynamicModule("MainMod");
Type generetedType = typeBldr.CreateType();
object generetedObject = Activator.CreateInstance(generetedType);
PropertyInfo[] properties = generetedType.GetProperties();
properties[0].SetValue(generetedObject , "Apple", null);
properties[1].SetValue(generetedObject , "Pear", null);