How to create a delegate to an instance method with a null target?
Solution 1
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);
}
}
Solution 2
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.
thecoop
Updated on June 15, 2022Comments
-
thecoop almost 2 years
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?