Hi everyone. I'm having trouble emitting a call to a delegate whose type is unfinished at the time of the emit. I'll elaborate: I'm trying to dynamically (i.e., with a TypeBuilder) create the following class:
public MyClass {
// Delegate type. The 'firstArgument' will be 'this', i.e., this is an open instance
// method: the implicit argument is here given explicitly, in 'firstArgument'.
// (below there's a link explaining open instance methods; I wasn't sure it would work
// within the code block and there's no way to preview!)
public delegate Object DirectReadAccessor<T>(T firstArgument);
// Array of delegates. T has been replaced with MyClass because the argument will be
// 'this', which is of type MyClass.
private static DirectReadAccessor<MyClass>[] directReadAccessors;
// Method that looks up a delegate in the array of delegates and calls it with 'this'.
public Object DirectRead(int index) {
directReadAccessors[index](this);
}
// Method that is called by the declaring type to pass an array with the MethodInfo
// of some methods. MyClass then creates delegates for these methods and stores them
// in the directReadAccessors array.
public static void InitializeClass(MethodInfo[] directReadAccessorsMInfo) {
int length = directReadAccessorsMInfo.Length;
Type[] typeArguments = new Type[] { typeof(MyClass) };
directReadAccessors = new DirectReadAccessor<MyClass>[length];
// For each method in directReadAccessorsMInfo...
for (int index = 0; index < length; index++) {
// Create a delegate and store it in directReadAccessors.
directReadAccessors[index] = (DirectReadAccessor<MyClass>)
Delegate.CreateDelegate(
DirectReadAccessor<MyClass>, // Type of the delegate.
null, // Specify null first argument so that it's *open* instance.
directReadAccessorsMInfo[index].MakeGenericMethod(typeArguments) // The method.
);
}
}
}* About open instance methods (and other things).
This has been tricky because MyClass doesn't exist when I'm trying to declare the field directReadAccessors, which is of type DirectReadAccessor<MyClass>[], or when I emit the method InitalizeClass, which again uses MyClass, that doesn't exist yet (that's what I'm creating). However, I've managed to do all this, but now I'm having trouble with method DirectRead, since I don't know how to call the delegate once I have it on the stack. Apparently what I need is the following emit:
ilGenerator.Emit(OpCodes.Callvirt, invokeMInfo);
where invokeMInfo is the method Invoke on DirectReadAccessor<MyClass>, and which I should obtain like so:
MethodInfo invokeMInfo = typeof(DirectReadAccessor<MyClass>).GetMethod(
"Invoke", // Name of the method.
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, // Binding attributes.
null, // Binder.
new Type[] { typeof(MyClass) }, // Types of the arguments.
null // Modifiers for the arguments.
);
Again, the problem is that neither MyClass nor DirectReadAccessor<MyClass> exist yet. I have the TypeBuilder for MyClass and the unfinished DirectReadAccessor type, which I've created like this:
directReadAccessorType = typeof(DirectReadAccessor<>).MakeGenericType(typeBuilder);
But if I try to call GetMethod("Invoke", ....) on directReadAccessorType as shown above I get a NotSupportedException, because I cannot obtain the method Invoke for an unfinished type. I've tested this assumption by making the same call after finalizing the type with:
typeBuilder.CreateType();
And indeed I do not get the exception in that case. However, I need to be able to get the Invoke method's MethodInfo before finalizing the type, while I'm emitting the code for InitializeClass.
It's a strange situation: I'll have the delegate when I need it, but I cannot produce the code to invoke it. Can anyone offer any help?
Thanks so much, and sorry for the lengthy post.
-------------------------------------------------------------------------------------------------------------------------
EDIT: answer to vulpes.
I tried your approach but since MyClass hasn't yet been created I get an ArgumentException from the first GetMethod saying MyClass must be a type provided at run time. In my case I only have the type builder, so I'm making the call like this:
MethodInfo mi = typeof(DirectReadAccessor<>).GetMethod(
"Invoke", // Name of the method.
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, // Binding attributes.
null, // Binder.
new Type[] { myClassTypeBuilder }, // <---- exception
null // Modifiers for the arguments.
); // use unbound generic type
Any ideas? Thanks again.
-------------------------------------------------------------------------------------------------------------------------
@vulpes: I've tried your second suggestion:
MethodInfo mi = typeof(DirectReadAccessor<>).GetMethod(
"Invoke", // Name of the method.
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, // Binding attributes.
null, // Binder.
typeof(DirectReadAccessor<>).GetGenericArguments(), // Types of the arguments
null // Modifiers for the arguments.
); // use unbound generic type
And now I get a BadImageFormatException: An attempt was made to load a program with incorrect format in method directRead. For some reason I cannot see even its IL code in Reflector; I guess it doesn't even consider the IL code to be valid. If I comment the line in which I call Invoke it does show the code. I've taken it and added the call at the appropriate point so that you can see it:
public Object DirectRead(int index) {
// Push 'this' onto the stack.
L_0000: ldarg.0
// Pop 'this' and push this.directReadAccessors
// (Note: Factory is the class from which I'm emitting the code of MyClass)
L_0001: ldfld class Factory/DirectReadAccessor<class MyClass>[] MyClass::directReadAccessors
// Push 'index'.
L_0006: ldarg.1
// Pop 'index' and this.directReadAccessors and push this.directReadAccessors[index], as
// an object reference. Now we have the delegate on top of the stack.
L_0007: ldelem.ref
// Push 'this', as a parameter for the delegate.
L_0008: ldarg.0
// Invoke the delegate: this.directReadAccessors[index](this)
// (I made up this line; this is how it should be, but it won't be like this because I
// don't have MyClass)
L_0009: callvirt instance void DirectReadAccessor<class MyClass>::Invoke(!0)
// Return.
L_000a: ret
}I've also tried...
MethodInfo mi = typeof(DirectReadAccessor<>).MakeGenericType(myClassTypeBuilder).GetMethod(
"Invoke", // Name of the method.
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, // Binding attributes.
null, // Binder.
typeof(DirectReadAccessor<>).GetGenericArguments(), // Types of the arguments
null // Modifiers for the arguments.
); // use unbound generic type
...just for kicks, because it didn't make much sense, and of course it didn't work. I get "NotSupportedException: Specified method is not compatible" on this line (where I call GetMethod).
Any suggestions? Thanks a lot.
-------------------------------------------------------------------------------------------------------------------------
@vulpes's 2nd edit
It's certainly a tough one to crack - a classic chicken and egg situation!
Exactly! This has created a lot of problems that gave me a really hard time, but I've managed to solve everything else. However I'm losing hope with this one, even though it seems something you should be able to do...
As for your suggestion, I tried
MethodInfo mi = typeof(DirectReadAccessor<>).GetMethod(
"Invoke", // Name of the method.
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, // Binding attributes.
null, // Binder.
directReadAccessorType.GetGenericArguments(), // Types of the arguments
null // Modifiers for the arguments.
); // use unbound generic type
but I get "type must be provided at run time" because directReadAccessorType is
typeof(DirectReadAccessor<>).MakeGenericType(myClassBuilder)
I've tried all combinations of the following instances on which to call GetMethod:
- 1. typeof(DirectReadAccessor<>).GetMethod(...)
- 2. typeof(DirectReadAccessor<>).MakeGenericType(myClassBuilder).GetMethod(...)
(same as directReadAccessorType.GetMethod(...)) - 3. directReadAccessorType.MakeGenericType(myClassBuilder).GetMethod(...)
And the following types to pass to GetMethod as 4th argument:
- a. new Type[] { myClassBuilder }
- b. typeof(DirectReadAccessor<>).GetGenericArguments()
- c. directReadAccessorType.GetGenericArguments()
(same as typeof(DirectReadAccessor<>).MakeGenericType(myClassBuilder).GetGenericArguments())
And I get the following results:
- 1a and 1c. "Type must be provided at run time", because they involve myClassBuilder.
- 1b. "BadImageFormatException"
- All the rest: not compatible.
(sorry about the weird formatting; it was the best I could do with the formats available :) )
As for declaring DirectReadAccessor<T> outside MyClass, I've tried it, and in fact DirectReadAccessor<T> exists, but it's generic, and I need to use DirectReadAccessor<MyClass> to get the Invoke method (or so I understand). The issue here is with MyClass, not with DirectReadAccessor.

2 answers
They answered this for me at stackoverflow. The following call works:
TypeBuilder.GetMethod(directReadAccessorType, typeof(DirectReadAccessor<>).GetMethod("Invoke"));P.S. Thanks to vulpes for taking the time to make suggestions!
answered 4 months ago by:
28
11603
Pleased to see you've found a solution which, like all good solutions, is gloriously simple ;) The strange thing is that we were virtually there but needed to use a simpler overload of GetMethod() to avoid the problem with the Type array - i.e. just needed : MethodInfo mi = typeof(DirectReadAccessor<>).GetMethod("Invoke"); followed by a call to the static method!
There's a static overload of TypeBuilder.GetMethod() which can be used with an uncreated type builder. If I've understood it correctly, the code you'll need is:
EDIT
As using the typebuilder for MyClass didn't work, I wonder if it would work if you called GetGenericArguments() on the open type object for the delegate?
SECOND EDIT
For good measure, I'd also try:
but, other than that, I'm out of suggestions for the moment.
One thing that might help simplify the situation is to make DirectReadAccessor<T> an independent type rather than a nested type of MyClass. If you did that, you'd be able to complete the building of the generic delegate type before moving on to build the type for MyClass.
answered 4 months ago by:
11603
28
Hi vulpes, thanks for your answer. I've edited my question to reply to it, since the comments allow no formatting. Please check the end of my question.
11603
Sorry, I forgot to address that point in my answer though I would have suggested using the typebuilder for MyClass in any case. See my edit for another suggestion, though with no great confidence.
28
Hi. I've tried your new suggestion, with no luck so far. The details are at the end of my question. Thanks again.
11603
It's certainly a tough one to crack - a classic chicken and egg situation! See my second edit for a couple more thoughts.
28
A tough one indeed. I've edited my answer again, but to sum it up: no luck whatsoever. If you have any other idea (even if it involves major restructuring) please let me know. In any case, thanks so much for your input.