A object instance, or null if the delegate invokes only static methods.
An instance method is a method that is associated with an instance of a class; a static method is a method that is associated with the class itself.
If the delegate invokes one or more instance methods, this property returns the target of the last instance method in the invocation list.
Example 1:
The following example gets the Delegate.Target property values for two delegates. The first delegate invokes a static method, and the second invokes an instance method.
C# Example
using System; public delegate string DelegatedMethod(string s); class MyClass { public static string StaticMethod(string s) { return ("Static method Arg=" + s); } public string InstanceMethod(string s) { return ("Instance method Arg=" + s); } } class TestClass { public static void Main() { MyClass myInstance = new MyClass(); //Create delegates from delegate type DelegatedMethod. DelegatedMethod delStatic = new DelegatedMethod(MyClass.StaticMethod); DelegatedMethod delInstance = new DelegatedMethod(myInstance.InstanceMethod); object t = delStatic.Target; Console.WriteLine ("Static target is {0}", t==null ? "null":t); t = delInstance.Target; Console.WriteLine ("Instance target is {0}", t==null ? "null":t); } }
The output is
Static target is nullExample 2:
The following example gets the Delegate.Target property value for three delegates created using instance methods, static methods, and a combination of the two.
C# Example
using System; class MyClass { public static string StaticMethod(string s) { return ("Static String " + s); } public string InstanceMethod(string s) { return ("Instance String " + s); } } class MyClass2 { public static string StaticMethod2(string s) { return ("Static String2 " + s); } public string InstanceMethod2(string s) { return ("Instance String2 " + s); } } public delegate string DelegatedMethod(string s); class TestClass { public static void Main() { DelegatedMethod delStatic = new DelegatedMethod(MyClass.StaticMethod); DelegatedMethod delStatic2 = new DelegatedMethod(MyClass2.StaticMethod2); MyClass myInstance = new MyClass(); DelegatedMethod delInstance = new DelegatedMethod(myInstance.InstanceMethod); MyClass2 myInstance2 = new MyClass2(); DelegatedMethod delInstance2 = new DelegatedMethod(myInstance2.InstanceMethod2); Delegate d = Delegate.Combine(delStatic, delInstance ); Delegate e = Delegate.Combine(delInstance,delInstance2); Delegate f = Delegate.Combine(delStatic, delStatic2 ); if (d!=null) { Console.WriteLine("Combined 1 static, 1 instance, same class:"); Console.WriteLine("target...{0}", d.Target == null ? "null" : d.Target); foreach(Delegate x in d.GetInvocationList()) Console.WriteLine("invoke element target: {0}",x.Target); } Console.WriteLine(""); if (e!=null) { Console.WriteLine("Combined 2 instance methods, different classes:"); Console.WriteLine("target...{0}", e.Target == null ? "null" : e.Target); foreach(Delegate x in e.GetInvocationList()) Console.WriteLine("invoke element target: {0}",x.Target); } Console.WriteLine(""); if (f!=null) { Console.WriteLine("Combined 2 static methods, different classes:"); Console.WriteLine("target...{0}", f.Target == null ? "null" : f.Target); foreach(Delegate x in f.GetInvocationList()) Console.WriteLine("invoke element target: {0}",x.Target); } } }
The output is
Combined 1 static, 1 instance, same class: