473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reflection emit consturctor problem.

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
2 5168
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
3
by: John Arthur | last post by:
Hi, I am reading about Reflection.Emit and I can't really imagine a real situation where I can use this. Can someone give me an example that can't be done without Reflection or an example of...
3
by: ayende | last post by:
How do I use Reflection.Emit to produce code similar to this? public void bar<T>() { } public void foo<J>() { bar<J>(); }
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
8
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; i just started research reflection and i'm wondering if i have an empty class file can i use reflection to add member variables and attributes dynamically and then instantiate the class? What...
9
by: Michael Sander | last post by:
Hello ng, anyone knowns how to add a reference to an assembly to System.Reflection.AssemblyBuilder? In System.Web.Compilation.AssemblyBuilder is a function like AddAssemblyReference, but not in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.