By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,661 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,661 IT Pros & Developers. It's quick & easy.

Reflection emit consturctor problem.

P: n/a
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;
}

}
--
Thanks
DEK
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
DEK <DE*@DEK.COM> wrote:
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 <<<<<)
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

P: n/a
DEK
The article <MP************************@news.microsoft.com>, that Jon
Skeet, sk***@pobox.com posted said,
DEK <DE*@DEK.COM> wrote:
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 <<<<<)


<snip>
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.
--
Thanks
DEK
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.