1

I want to create a Comparison<>-Delegate via reflection in code. I have this:

var returnType = typeof (Int32);
var parameters = typeof(Comparison<>).GetMethod("Invoke").GetParameters().Select(x => x.ParameterType).ToArray();

AppDomain domain = AppDomain.CurrentDomain;
AssemblyName aname = new AssemblyName("MyEmissions");
AssemblyBuilder assemBuilder = domain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule("MainModule", "MyEmissions.dll");

TypeBuilder tb = modBuilder.DefineType("Widget", TypeAttributes.Public);
MethodBuilder mb = tb.DefineMethod("Echo", MethodAttributes.Public | MethodAttributes.Static);

mb.SetSignature(returnType, null, null, parameters, null, null);

ILGenerator gen = mb.GetILGenerator();
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Ret);
var foo = MulticastDelegate.CreateDelegate(typeof(Comparison<>), mb);

The last line throws a ArgumentException: MethodInfo must be a runtime MethodInfo object. I'm new to reflection and emit and have a feeling that only a small step is missing!?

Edit:

I'm not bound to creating a new Assembly - I also tried DynamicMethod:

var returnType = typeof (Int32);
var parameters = typeof(Comparison<>).GetMethod("Invoke").GetParameters().Select(x => x.ParameterType).ToArray();
var handler = new DynamicMethod("", returnType, parameters);
var generator = handler.GetILGenerator();

foreach (var parameter in parameters)
{
    var localVariable = generator.DeclareLocal(parameter);
    generator.Emit(OpCodes.Ldloc, localVariable);
}

if (returnType != null)
{
    var returnValue = generator.DeclareLocal(returnType);
    generator.Emit(OpCodes.Ldloc, returnValue);
}

generator.Emit(OpCodes.Ret);

handler.CreateDelegate(typeof(Comparison<>));

Throws a BadImageFormatException :/


Solution:

var returnType = typeof (Int32);
var methodParameters = typeof(Comparison<>).GetMethod("Invoke").GetParameters().Select(x => x.ParameterType.ToString()).ToArray();

AppDomain domain = AppDomain.CurrentDomain;
AssemblyName aname = new AssemblyName("MyEmissions");
AssemblyBuilder assemBuilder = domain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder modBuilder = assemBuilder.DefineDynamicModule("MainModule", "MyEmissions.dll");

TypeBuilder tb = modBuilder.DefineType("Widget", TypeAttributes.Public);
MethodBuilder mb = tb.DefineMethod("Echo", MethodAttributes.Public | MethodAttributes.Static);

GenericTypeParameterBuilder[] typeParameters = mb.DefineGenericParameters(methodParameters);

mb.SetReturnType(returnType);
mb.SetParameters(typeParameters);

ILGenerator gen = mb.GetILGenerator();
gen.Emit(OpCodes.Ldnull);
gen.Emit(OpCodes.Ret);
var dt = tb.CreateType();

var mi = dt.GetMethod("Echo");
var gm = mi.MakeGenericMethod(new[] { typeof(string), typeof(string) });

var parameter = MulticastDelegate.CreateDelegate(typeof(Comparison<string>), gm);
Papa Mufflon
  • 17,558
  • 5
  • 27
  • 35
  • 1
    I *suspect* you need to call `TypeBuilder.CreateType`, and then get the method from that. Do you definitely *need* to create a new assembly like this though? I suspect `DynamicMethod` would be a better fit for you. – Jon Skeet Nov 12 '11 at 10:26
  • check this post in stack overflow may help you [Reflection.Emit: How to convert MethodBuilder to RuntimeMethodInfo reliably?][1] [1]: http://stackoverflow.com/questions/3031183/reflection-emit-how-to-convert-methodbuilder-to-runtimemethodinfo-reliably – DeveloperX Nov 12 '11 at 10:32
  • Can you provide some psuedo C# of what you are trying to emit? – leppie Nov 12 '11 at 11:14
  • @JonSkeet: You suspect correctly :) – leppie Nov 12 '11 at 11:14

2 Answers2

2

Others already pointed out some errors in your code. But there is another bigger issue: you're trying to create generic method and unbound generic delegate.

In the case of DynamicMethod, you can't create generic method at all. In the case of dynamic assembly, it is possible, but you have to use DefineGenericParameters().

If you managed to create a generic method somehow, you can't create an unbound generic delegate. That is, you can't create a delegate of type Comparison<T>, like you're trying to do. You have to substitute some specific type for T. So, for example, you could create Comparison<int>.

Also, I find twiddling with CIL quite hard, especially if you don't have much experience with it. It might be much easier to create your delegate by creating a Expression and compiling that.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514
0

As for your second example.

What you are doing is correct. BUT the code you emit is incorrect. That is why you are getting the BadImageFormatException.

Currently, you have values left on the stack left by:

generator.Emit(OpCodes.Ldloc, localVariable);

To remedy, either not load them or pop them from the stack again.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • Hmm - if I comment out the foreach-loop putting variables on the stack, a BatImageFormatException will still be thrown. – Papa Mufflon Nov 12 '11 at 11:28
  • @PapaMufflon, the exception message is “The signature is incorrect.” That's because you can't create generic method using `DynamicMethod`. See my answer for details. – svick Nov 12 '11 at 13:28
  • @svick: Interesting :) (Good spot +1 for answer) – leppie Nov 12 '11 at 13:31