本文整理匯總了C#中System.Delegate.CreateDelegate方法的典型用法代碼示例。如果您正苦於以下問題:C# Delegate.CreateDelegate方法的具體用法?C# Delegate.CreateDelegate怎麽用?C# Delegate.CreateDelegate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Delegate
的用法示例。
在下文中一共展示了Delegate.CreateDelegate方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: D1
//引入命名空間
using System;
using System.Reflection;
using System.Security.Permissions;
// Declare three delegate types for demonstrating the combinations
// of static versus instance methods and open versus closed
// delegates.
//
public delegate void D1(C c, string s);
public delegate void D2(string s);
public delegate void D3();
// A sample class with an instance method and a static method.
//
public class C
{
private int id;
public C(int id) { this.id = id; }
public void M1(string s)
{
Console.WriteLine("Instance method M1 on C: id = {0}, s = {1}",
this.id, s);
}
public static void M2(string s)
{
Console.WriteLine("Static method M2 on C: s = {0}", s);
}
}
public class Example
{
public static void Main()
{
C c1 = new C(42);
// Get a MethodInfo for each method.
//
MethodInfo mi1 = typeof(C).GetMethod("M1",
BindingFlags.Public | BindingFlags.Instance);
MethodInfo mi2 = typeof(C).GetMethod("M2",
BindingFlags.Public | BindingFlags.Static);
D1 d1;
D2 d2;
D3 d3;
Console.WriteLine("\nAn instance method closed over C.");
// In this case, the delegate and the
// method must have the same list of argument types; use
// delegate type D2 with instance method M1.
//
Delegate test =
Delegate.CreateDelegate(typeof(D2), c1, mi1, false);
// Because false was specified for throwOnBindFailure
// in the call to CreateDelegate, the variable 'test'
// contains null if the method fails to bind (for
// example, if mi1 happened to represent a method of
// some class other than C).
//
if (test != null)
{
d2 = (D2) test;
// The same instance of C is used every time the
// delegate is invoked.
d2("Hello, World!");
d2("Hi, Mom!");
}
Console.WriteLine("\nAn open instance method.");
// In this case, the delegate has one more
// argument than the instance method; this argument comes
// at the beginning, and represents the hidden instance
// argument of the instance method. Use delegate type D1
// with instance method M1.
//
d1 = (D1) Delegate.CreateDelegate(typeof(D1), null, mi1);
// An instance of C must be passed in each time the
// delegate is invoked.
//
d1(c1, "Hello, World!");
d1(new C(5280), "Hi, Mom!");
Console.WriteLine("\nAn open static method.");
// In this case, the delegate and the method must
// have the same list of argument types; use delegate type
// D2 with static method M2.
//
d2 = (D2) Delegate.CreateDelegate(typeof(D2), null, mi2);
// No instances of C are involved, because this is a static
// method.
//
d2("Hello, World!");
d2("Hi, Mom!");
Console.WriteLine("\nA static method closed over the first argument (String).");
// The delegate must omit the first argument of the method.
// A string is passed as the firstArgument parameter, and
// the delegate is bound to this string. Use delegate type
// D3 with static method M2.
//
d3 = (D3) Delegate.CreateDelegate(typeof(D3),
"Hello, World!", mi2);
// Each time the delegate is invoked, the same string is
// used.
d3();
}
}
輸出:
An instance method closed over C. Instance method M1 on C: id = 42, s = Hello, World! Instance method M1 on C: id = 42, s = Hi, Mom! An open instance method. Instance method M1 on C: id = 42, s = Hello, World! Instance method M1 on C: id = 5280, s = Hi, Mom! An open static method. Static method M2 on C: s = Hello, World! Static method M2 on C: s = Hi, Mom! A static method closed over the first argument (String). Static method M2 on C: s = Hello, World!
示例2: MyMethod
//引入命名空間
using System;
using System.Reflection;
// Define two classes to use in the demonstration, a base class and
// a class that derives from it.
//
public class Base {}
public class Derived : Base
{
// Define a static method to use in the demonstration. The method
// takes an instance of Base and returns an instance of Derived.
// For the purposes of the demonstration, it is not necessary for
// the method to do anything useful.
//
public static Derived MyMethod(Base arg)
{
Base dummy = arg;
return new Derived();
}
}
// Define a delegate that takes an instance of Derived and returns an
// instance of Base.
//
public delegate Base Example(Derived arg);
class Test
{
public static void Main()
{
// The binding flags needed to retrieve MyMethod.
BindingFlags flags = BindingFlags.Public | BindingFlags.Static;
// Get a MethodInfo that represents MyMethod.
MethodInfo minfo = typeof(Derived).GetMethod("MyMethod", flags);
// Demonstrate contravariance of parameter types and covariance
// of return types by using the delegate Example to represent
// MyMethod. The delegate binds to the method because the
// parameter of the delegate is more restrictive than the
// parameter of the method (that is, the delegate accepts an
// instance of Derived, which can always be safely passed to
// a parameter of type Base), and the return type of MyMethod
// is more restrictive than the return type of Example (that
// is, the method returns an instance of Derived, which can
// always be safely cast to type Base).
//
Example ex =
(Example) Delegate.CreateDelegate(typeof(Example), minfo);
// Execute MyMethod using the delegate Example.
//
Base b = ex(new Derived());
}
}
示例3: D
//引入命名空間
using System;
using System.Reflection;
using System.Security.Permissions;
// Declare a delegate type. The object of this code example
// is to show all the methods this delegate can bind to.
//
public delegate void D(C c);
// Declare two sample classes, C and F. Class C has an ID
// property so instances can be identified.
//
public class C
{
private int id;
public int ID { get { return id; }}
public C(int id) { this.id = id; }
public void M1(C c)
{
Console.WriteLine("Instance method M1(C c) on C: this.id = {0}, c.ID = {1}",
this.id, c.ID);
}
public void M2()
{
Console.WriteLine("Instance method M2() on C: this.id = {0}",
this.id);
}
public static void M3(C c)
{
Console.WriteLine("Static method M3(C c) on C: c.ID = {0}", c.ID);
}
public static void M4(C c1, C c2)
{
Console.WriteLine("Static method M4(C c1, C c2) on C: c1.ID = {0}, c2.ID = {1}",
c1.ID, c2.ID);
}
}
public class F
{
public void M1(C c)
{
Console.WriteLine("Instance method M1(C c) on F: c.ID = {0}",
c.ID);
}
public static void M3(C c)
{
Console.WriteLine("Static method M3(C c) on F: c.ID = {0}", c.ID);
}
public static void M4(F f, C c)
{
Console.WriteLine("Static method M4(F f, C c) on F: c.ID = {0}",
c.ID);
}
}
public class Example
{
public static void Main()
{
C c1 = new C(42);
C c2 = new C(1491);
F f1 = new F();
D d;
// Instance method with one argument of type C.
MethodInfo cmi1 = typeof(C).GetMethod("M1");
// Instance method with no arguments.
MethodInfo cmi2 = typeof(C).GetMethod("M2");
// Static method with one argument of type C.
MethodInfo cmi3 = typeof(C).GetMethod("M3");
// Static method with two arguments of type C.
MethodInfo cmi4 = typeof(C).GetMethod("M4");
// Instance method with one argument of type C.
MethodInfo fmi1 = typeof(F).GetMethod("M1");
// Static method with one argument of type C.
MethodInfo fmi3 = typeof(F).GetMethod("M3");
// Static method with an argument of type F and an argument
// of type C.
MethodInfo fmi4 = typeof(F).GetMethod("M4");
Console.WriteLine("\nAn instance method on any type, with an argument of type C.");
// D can represent any instance method that exactly matches its
// signature. Methods on C and F are shown here.
//
d = (D) Delegate.CreateDelegate(typeof(D), c1, cmi1);
d(c2);
d = (D) Delegate.CreateDelegate(typeof(D), f1, fmi1);
d(c2);
Console.WriteLine("\nAn instance method on C with no arguments.");
// D can represent an instance method on C that has no arguments;
// in this case, the argument of D represents the hidden first
// argument of any instance method. The delegate acts like a
// static method, and an instance of C must be passed each time
// it is invoked.
//
d = (D) Delegate.CreateDelegate(typeof(D), null, cmi2);
d(c1);
Console.WriteLine("\nA static method on any type, with an argument of type C.");
// D can represent any static method with the same signature.
// Methods on F and C are shown here.
//
d = (D) Delegate.CreateDelegate(typeof(D), null, cmi3);
d(c1);
d = (D) Delegate.CreateDelegate(typeof(D), null, fmi3);
d(c1);
Console.WriteLine("\nA static method on any type, with an argument of");
Console.WriteLine(" that type and an argument of type C.");
// D can represent any static method with one argument of the
// type the method belongs and a second argument of type C.
// In this case, the method is closed over the instance of
// supplied for the its first argument, and acts like an instance
// method. Methods on F and C are shown here.
//
d = (D) Delegate.CreateDelegate(typeof(D), c1, cmi4);
d(c2);
Delegate test =
Delegate.CreateDelegate(typeof(D), f1, fmi4, false);
// This final example specifies false for throwOnBindFailure
// in the call to CreateDelegate, so the variable 'test'
// contains Nothing if the method fails to bind (for
// example, if fmi4 happened to represent a method of
// some class other than F).
//
if (test != null)
{
d = (D) test;
d(c2);
}
}
}
輸出:
An instance method on any type, with an argument of type C. Instance method M1(C c) on C: this.id = 42, c.ID = 1491 Instance method M1(C c) on F: c.ID = 1491 An instance method on C with no arguments. Instance method M2() on C: this.id = 42 A static method on any type, with an argument of type C. Static method M3(C c) on C: c.ID = 42 Static method M3(C c) on F: c.ID = 42 A static method on any type, with an argument of that type and an argument of type C. Static method M4(C c1, C c2) on C: c1.ID = 42, c2.ID = 1491 Static method M4(F f, C c) on F: c.ID = 1491