tags:

views:

1024

answers:

2

I've noticed that the Delegate class has a Target property, that (presumably) returns the instance the delegate method will execute on. I want to do something like this:

void PossiblyExecuteDelegate(Action<int> method)
{
    if (method.Target == null)   
    {
        // delegate instance target is null
        // do something
    }
    else
    {
         method(10);
         // do something else
    }
}

When calling it, I want to do something like:

class A
{
    void Method(int a) {}

    static void Main(string[] args)
    {
        A a = null;
        Action<int> action = a.Method;
        PossiblyExecuteDelegate(action);
    }
}

But I get an ArgumentException (Delegate to an instance method cannot have a null 'this') when I try to construct the delegate. Is what I want to do possible, and how can I do it?

A: 

In order to do this you would have to pass a static method to PossiblyExecuteDelegate(). This will give you a null Target.

class A
{
    void Method(int a) {}
    static void Method2(int a) {}

    static void Main(string[] args)
    {
        PossiblyExecuteDelegate(A.Method2);

        A a = new A();

        PossiblyExecuteDelegate(a.Method);
    }
}

Edit: It is possible to pass a delegate to an instance method with no target via reflection, but not using standard compiled code.

Adam Robinson
I mean test whether the instance method would be called on a null instance, if it was called. The problem is getting the instance in the first place. I do remember reading something about open delegates, but I can't find hide nor hair of it now...
thecoop
Just found the docs again - you can create an open instance delegate using a Delegate.CreateDelegate overload (posted an answer with details)
thecoop
@thecoop: Yes, I should have noted that this would be possible with reflection, but not with any compile-time-safe code. Answer edited.
Adam Robinson
A: 

Ahah! found it!

You can create an open instance delegate using a CreateDelegate overload, using a delegate with the implicit 'this' first argument explicitly specified:

delegate void OpenInstanceDelegate(A instance, int a);

class A
{
    public void Method(int a) {}

    static void Main(string[] args)
    {
        A a = null;
        MethodInfo method = typeof(A).GetMethod("Method");
        OpenInstanceDelegate action = (OpenInstanceDelegate)Delegate.CreateDelegate(typeof(OpenInstanceDelegate), a, method);

        PossiblyExecuteDelegate(action);
    }
}
thecoop
Yes, as I edited my own response, this is possible with reflection, but not with standard compiled .NET code. Is your concern that people will be passing you reflectively declared delegates?
Adam Robinson