views:

176

answers:

3

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've declared the following delegate type:

// Delegate type. The 'firstArgument' will be 'this', i.e., this is an open
// instance method: the implicit argument is here given explicitly, in
// 'firstArgument'. (See link below for explanation on open instance delegates).
public delegate Object DirectReadAccessor<T>(T firstArgument);

And now I'm trying to dynamically (i.e., with a TypeBuilder) create the following class:

public MyClass {

    // 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 i) {
        directReadAccessors[i](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 i = 0; i < length; i++) {
            // Create a delegate and store it in directReadAccessors.
            directReadAccessors[i] = (DirectReadAccessor<MyClass>)
                   Delegate.CreateDelegate(
                          DirectReadAccessor<MyClass>, // Type of the delegate.
                          null, // Specify null first argument so that it's
                                // *open* instance.
                          directReadAccessorsMInfo[i].MakeGenericMethod(typeArguments) // The method.
                    );
        }
    }

}

*on open instance delegates.

This has been tricky because MyClass doesn't exist when I'm trying to declare the field directReadAccessors, which is of type DirectReadAccessor[], 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, 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 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.

A: 

Have you tried to compile the generic code and look at it using ILDASM ? It should show you the correct IL and hopefully the right emit to generate.

Timores
Hi. Yeah, where I say "Apparently what I need is the following emit" above, that emit was obtained like you suggest. The problem is I thought I could just load the delegate onto the stack at runtime (which I do) and call it as-is, without having to provide an Invoke method specific for the delegate's type when I emit the code (since the type doesn't exist yet).
Alix
A: 

Would it make sense to minimize the amount of code in the generated class ?

By that, I mean that the generated class could implement an interface that gives access to its data. Through it, you could implement additional features that could be coded in C# instead of IL.

Timores
Hi. The problem is that this is a system which takes classes written by the users (in this case application programmers) and extends them with some functionality, and above all makes sure the access to the fields is done safely (through the accessors whose delegates I have in the array directReadAccessors). All this must be done automatically, and I don't want to force the users to write interfaces, etc. I must do all this by dynamically emitting the classes. If you have any idea on how to do this better please let me know. Thanks!
Alix
I am not sure I understood your requirements (it seems that application programmers should be able to implement an interface with one method), but another way to tackle the problem could be to generate C# and compile it by calling csc and then loading the resulting DLL.Or providing a build step or script that does it, so that the application programmers reference this generated DLL.
Timores
"this is a system which takes classes written by the users" - *shivers*
Mitch Wheat
@Timores: I'm sure they'd be able to implement it, I just don't want them to change their code any more than I have to. But kvb has already solved my problem, so no worries! Thanks for your suggestions!
Alix
@Mitch: Exactly :)
Alix
+2  A: 

That's a tricky problem. I think that you should be able to use TypeBuilder.GetMethod to get the MethodInfo you need, using a call along the lines of

TypeBuilder.GetMethod(directReadAccessorType, 
    typeof(DirectReadAccessor<>).GetMethod("Invoke"));
kvb
Oh my god, that is exactly it. It works! Thanks so so so so so so so so much, I've been struggling with this for days! (it was getting really frustrating; I'm having trouble finding the library calls I need, like this one). Thanks again, you really made my day! :)
Alix
@Alix - I'm glad I was able to help. Unfortunately, the static `GetMethod`, `GetField`, and `GetConstructor` methods on `TypeBuilder` are not very discoverable so it's easy to get stuck when doing complex stuff with Reflection.Emit and generics.
kvb
Tell me about it :). I've been programming for years (not in C# though) and I normally don't need to ask much on forums, but since I got started with C# a couple of weeks ago I've had to ask quite a few questions. Granted, some of the stuff I'm trying to do is kinda unusual :)
Alix