public static void Main() { var x = new MyClass <Bar>(); FooDelegate test = x.Foo; test("Do It"); }
static void Main(string[] args) { FooDelegate fooDelegate = new FooDelegate(foo); fooDelegate("Hello from delegate"); FooDelegate otherDelegate = new FooDelegate(message => Console.WriteLine("Lambda: "+ message)); otherDelegate("this running delegate was instantiated from a C# lambda expression"); run(x => Console.WriteLine(x),"Hello World"); // this is neat }
static void Main(string[] args) { Console.WriteLine("主线程." + Thread.CurrentThread.ManagedThreadId); FooDelegate fooDelegate = Foo; fooDelegate.BeginInvoke("Hello world.", FooComepleteCallback, fooDelegate); Console.WriteLine("主线程继续执行..." + Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); }
static void Main(string[] args) { var foo1 = new Foo("Foo One"); FooDelegate del1 = foo1.Hello; FooDelegate del2 = Foo.StaticHello; FooDelegate del3 = foo1.Bye; del1(); del2(); del3(); }
static void Main(string[] args) { FooDelegate bar1,bar2,bar3,bar4,bar5; bar1 = new FooDelegate(foo1); bar2 = new FooDelegate(foo2); bar3 = new FooDelegate(foo3); bar4 = bar1 + bar2 + bar3; bar4(); bar5 = bar4 - bar2; bar5(); }
public static bool RunTest() { IntPtr funcPtr = GetFuncPtr(); var myDelegate = Marshal.GetDelegateForFunctionPointer <MyDelegate>(funcPtr); FooDelegate myFooDelegate = FooStatic; myFooDelegate += new Interop().Foo; IntPtr dFuncPtr = Marshal.GetFunctionPointerForDelegate <FooDelegate>(myFooDelegate, out IntPtr dThisPtr, out _); InvokeDelegateFuncPtr(dFuncPtr, dThisPtr); return(GetLastError() == 0 && myDelegate(123) == 123 && fooStaticCalled && fooCalled); }
static void Main(string[] args) { Console.WriteLine("主线程" + Thread.CurrentThread.ManagedThreadId); FooDelegate fooDelegate = Foo; IAsyncResult result = fooDelegate.BeginInvoke("Hello World.", null, null); Console.WriteLine("主线程继续执行..."); //当使用BeginInvoke异步调用方法时,如果方法未执行完,EndInvoke方法就会一直阻塞,直到被调用的方法执行完毕 int n = fooDelegate.EndInvoke(result); Console.WriteLine("回到主线程" + Thread.CurrentThread.ManagedThreadId); Console.WriteLine("结果是" + n); Console.ReadKey(true); }
public static void Inject() { var oldImpl = Foo; Foo = i => { try { BeginDebug(); return(oldImpl(i)); } finally { EndDebug(); } }; }
[Category ("NotWorking")] // #10539 #endif public void Virtual () { // Delegate with abstract method, no target FooDelegate del = (FooDelegate)Delegate.CreateDelegate (typeof (FooDelegate), typeof (Iface).GetMethod ("retarg")); C c = new C (); Assert.AreEqual ("Hello", del (c, "Hello")); // Combination with normal delegates FooDelegate del2 = (FooDelegate)Delegate.CreateDelegate (typeof (FooDelegate), typeof (Iface).GetMethod ("retarg")); FooDelegate del3 = new FooDelegate (c.retarg2); FooDelegate del4 = (FooDelegate)Delegate.Combine (del2, del3); Assert.AreEqual ("Hello2", del4 (c, "Hello")); // Delegate with virtual method, no target FooDelegate2 del5 = (FooDelegate2)Delegate.CreateDelegate (typeof (FooDelegate2), typeof (B).GetMethod ("retarg3")); Assert.AreEqual ("Hello2", del5 (c, "Hello")); }
private static void Stub(string name, FooDelegate fooDelegate) { fooDelegate(name); }
#pragma warning disable RCS1213 private static void Foo(FooDelegate callback) #pragma warning restore RCS1213 { }
protected X () { foo = new FooDelegate (Foo); }
public static int Test(FooDelegate foo) { return(foo()); }
protected X() { foo = new FooDelegate(Foo); }
public static void run(FooDelegate foo, string message) { foo(message); }