Reflection emit consturctor problem.

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;
}

}
 
J

Jon Skeet

DEK said:
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 <<<<<)

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[]);

Well, this is *part* of the problem - you've got 5 arguments here, but
your constructor only takes 4.

However, your creation call is flawed as well:
object obj = season.CreateInstance
(className,true,BindingFlags.Public, null,
args,null,null);

You need to specify BindingFlags.Public|BindingFlags.Instance here.
 
D

DEK

that Jon said:
DEK said:
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 <<<<<)

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[]);

Well, this is *part* of the problem - you've got 5 arguments here, but
your constructor only takes 4.

However, your creation call is flawed as well:
object obj = season.CreateInstance
(className,true,BindingFlags.Public, null,
args,null,null);

You need to specify BindingFlags.Public|BindingFlags.Instance here.
Thanks, I was coding in the wee small hours last night and probably
should have went to bed earlier. Ill give it a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top