D
DEK
I'm creating a dynamic assembly which has one type with one constructor,
the type inherits a base class Season, which is in the current assembly.
I am trying to emit a constructor which simply calls the base
constructor with the 4 args passed in, but I get this error Message at
the second last line of the method CreateSeason (Marked with <<<<<)
An unhandled exception of type 'System.MissingMethodException' occurred
in mscorlib.dll
Additional information: Constructor on type FormulaOneSeason not found.
/// <summary>
/// Class for building dynamic seasons
/// </summary>
public class TypeCreator
{
public static Season CreateSeason(string name,DateTime
startDate, DateTime endDate, string[] infoColumns, string
[] infoValues)
{
// Create the names
string className = Regex.Replace(name, @"\s", "");
string fileName = className + Game.GetInstance
().GUID.ToString("B") + ".dll";
// Create assembly name
AssemblyName an = new AssemblyName();
an.Name = className;
// Get the assembly builder from current appdomain
AssemblyBuilder ab = Thread.GetDomain
().DefineDynamicAssembly(an,
AssemblyBuilderAccess.RunAndSave);
// Get module builder
ModuleBuilder mb = ab.DefineDynamicModule
(className, fileName,true);
// Get Type builder
TypeBuilder tb = mb.DefineType
(className,TypeAttributes.Class |
TypeAttributes.Public, typeof(Season));
object[] args = new object[] {name,startDate,
endDate, infoColumns, infoValues};
// Create constructor args
Type[] constructorArgs = new Type[4];
constructorArgs[0] = typeof (DateTime);
constructorArgs[1] = typeof (DateTime);
constructorArgs[2] = typeof (string[]);
constructorArgs[3] = typeof (string[]);
// Get constructor builder
ConstructorBuilder cb = tb.DefineConstructor
(MethodAttributes.Public,
CallingConventions.Standard , constructorArgs );
// Generate opcodes for constructor
ILGenerator ilGen = cb.GetILGenerator();
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldarg_1);
ilGen.Emit(OpCodes.Ldarg_2);
ilGen.Emit(OpCodes.Ldarg_3);
ilGen.Emit(OpCodes.Call, typeof
(Season).GetConstructor(constructorArgs));
ilGen.Emit(OpCodes.Ret);
// Bake the type
Type seasonType = tb.CreateType();
ab.Save(fileName);
Assembly season = Assembly.LoadFrom(fileName);
object obj = season.CreateInstance
(className,true,BindingFlags.Public, null,
args,null,null); <<<<<
return (Season) obj;
}
}
the type inherits a base class Season, which is in the current assembly.
I am trying to emit a constructor which simply calls the base
constructor with the 4 args passed in, but I get this error Message at
the second last line of the method CreateSeason (Marked with <<<<<)
An unhandled exception of type 'System.MissingMethodException' occurred
in mscorlib.dll
Additional information: Constructor on type FormulaOneSeason not found.
/// <summary>
/// Class for building dynamic seasons
/// </summary>
public class TypeCreator
{
public static Season CreateSeason(string name,DateTime
startDate, DateTime endDate, string[] infoColumns, string
[] infoValues)
{
// Create the names
string className = Regex.Replace(name, @"\s", "");
string fileName = className + Game.GetInstance
().GUID.ToString("B") + ".dll";
// Create assembly name
AssemblyName an = new AssemblyName();
an.Name = className;
// Get the assembly builder from current appdomain
AssemblyBuilder ab = Thread.GetDomain
().DefineDynamicAssembly(an,
AssemblyBuilderAccess.RunAndSave);
// Get module builder
ModuleBuilder mb = ab.DefineDynamicModule
(className, fileName,true);
// Get Type builder
TypeBuilder tb = mb.DefineType
(className,TypeAttributes.Class |
TypeAttributes.Public, typeof(Season));
object[] args = new object[] {name,startDate,
endDate, infoColumns, infoValues};
// Create constructor args
Type[] constructorArgs = new Type[4];
constructorArgs[0] = typeof (DateTime);
constructorArgs[1] = typeof (DateTime);
constructorArgs[2] = typeof (string[]);
constructorArgs[3] = typeof (string[]);
// Get constructor builder
ConstructorBuilder cb = tb.DefineConstructor
(MethodAttributes.Public,
CallingConventions.Standard , constructorArgs );
// Generate opcodes for constructor
ILGenerator ilGen = cb.GetILGenerator();
ilGen.Emit(OpCodes.Ldarg_0);
ilGen.Emit(OpCodes.Ldarg_1);
ilGen.Emit(OpCodes.Ldarg_2);
ilGen.Emit(OpCodes.Ldarg_3);
ilGen.Emit(OpCodes.Call, typeof
(Season).GetConstructor(constructorArgs));
ilGen.Emit(OpCodes.Ret);
// Bake the type
Type seasonType = tb.CreateType();
ab.Save(fileName);
Assembly season = Assembly.LoadFrom(fileName);
object obj = season.CreateInstance
(className,true,BindingFlags.Public, null,
args,null,null); <<<<<
return (Season) obj;
}
}