Click here to Skip to main content
15,892,059 members
Home / Discussions / C#
   

C#

 
AnswerRe: Generic List Pin
Pete O'Hanlon25-Feb-10 23:57
mvePete O'Hanlon25-Feb-10 23:57 
GeneralRe: Generic List [modified] Pin
spankyleo12326-Feb-10 0:12
spankyleo12326-Feb-10 0:12 
GeneralRe: Generic List Pin
Paulo Zemek26-Feb-10 1:47
mvaPaulo Zemek26-Feb-10 1:47 
GeneralRe: Generic List Pin
Pete O'Hanlon26-Feb-10 1:56
mvePete O'Hanlon26-Feb-10 1:56 
GeneralRe: Generic List Pin
Searril26-Feb-10 4:38
Searril26-Feb-10 4:38 
Question[SOLVED] I can't figure out EmitCalli of ILGenerator [modified] Pin
blackblizzard25-Feb-10 23:24
blackblizzard25-Feb-10 23:24 
AnswerRe: I can't figure out EmitCalli of ILGenerator Pin
Kythen26-Feb-10 13:08
Kythen26-Feb-10 13:08 
Question[SOLVED] How can I emit a call to a delegate whose type is unfinished at the time of the emit? [modified] Pin
blackblizzard28-Feb-10 6:28
blackblizzard28-Feb-10 6:28 
Thanks for your answer, Kythen.
However, this is bad news for me... Frown | :(
I need to call a method that is already on the stack. I cannot load it with ldvirtftn because I don't have it at the time when I'm generating the code.
The situation is like this: I'm trying to generate 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 couldn't get it to work within
    // the code block)
    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 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.
                    );
        }
    }

}


* 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.Publ<code></code>ic | 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?
modified on Friday, March 5, 2010 3:23 AM

AnswerRe: How can I emit a call to a delegate whose type is unfinished at the time of the emit? Pin
blackblizzard4-Mar-10 21:23
blackblizzard4-Mar-10 21:23 
QuestionWebbrowser Control - selected Image Pin
hoernchenmeister25-Feb-10 21:25
hoernchenmeister25-Feb-10 21:25 
QuestionHow to Read a .doc in C# Pin
gouthami chintala25-Feb-10 20:48
gouthami chintala25-Feb-10 20:48 
AnswerMessage Removed Pin
25-Feb-10 20:50
stancrm25-Feb-10 20:50 
GeneralMessage Removed Pin
26-Feb-10 2:34
gouthami chintala26-Feb-10 2:34 
GeneralRe: How to Read a .doc in C# Pin
Dave Kreskowiak26-Feb-10 3:26
mveDave Kreskowiak26-Feb-10 3:26 
AnswerRe: How to Read a .doc in C# Pin
Mohammad Dayyan25-Feb-10 20:53
Mohammad Dayyan25-Feb-10 20:53 
AnswerRe: How to Read a .doc in C# Pin
Saksida Bojan25-Feb-10 21:03
Saksida Bojan25-Feb-10 21:03 
AnswerRe: How to Read a .doc in C# Pin
Kythen26-Feb-10 12:34
Kythen26-Feb-10 12:34 
QuestionCrystal Report XI issue Pin
Raza Hussain25-Feb-10 20:47
Raza Hussain25-Feb-10 20:47 
AnswerRe: Crystal Report XI issue Pin
Mohammad Dayyan25-Feb-10 20:57
Mohammad Dayyan25-Feb-10 20:57 
GeneralRe: Crystal Report XI issue Pin
Raza Hussain28-Feb-10 23:11
Raza Hussain28-Feb-10 23:11 
Questionblood Pressure Pin
Jassim Rahma25-Feb-10 20:44
Jassim Rahma25-Feb-10 20:44 
AnswerMessage Closed Pin
25-Feb-10 20:53
stancrm25-Feb-10 20:53 
GeneralRe: blood Pressure Pin
Jassim Rahma25-Feb-10 20:56
Jassim Rahma25-Feb-10 20:56 
AnswerRe: blood Pressure Pin
AspDotNetDev25-Feb-10 21:55
protectorAspDotNetDev25-Feb-10 21:55 
GeneralRe: blood Pressure Pin
Jassim Rahma26-Feb-10 18:31
Jassim Rahma26-Feb-10 18:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.