本文整理汇总了C#中MyDelegate类的典型用法代码示例。如果您正苦于以下问题:C# MyDelegate类的具体用法?C# MyDelegate怎么用?C# MyDelegate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MyDelegate类属于命名空间,在下文中一共展示了MyDelegate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
MyDelegate a, b, c, d;
// Create the delegate object a that references
// the method Hello:
a = new MyDelegate(Hello); // Ex A1 - Atribuição de Métodos
// Create the delegate object b that references
// the method Goodbye:
b = new MyDelegate(Goodbye); // Ex A1 - Atribuição de Métodos
// The two delegates, a and b, are composed to form c:
c = a + b; // Ex A1 - Adição de Métodos
// Remove a from the composed delegate, leaving d,
// which calls only the method Goodbye:
d = c - a; // Ex A1 - Subtração de métodos
Console.WriteLine("Invoking delegate a:");
a("A"); // Ex A1 - Invocação
Console.WriteLine("Invoking delegate b:");
b("B"); // Ex A1 - Invocação
Console.WriteLine("Invoking delegate c:");
c("C"); // Ex A1 - Invocação // Ex A2 - Não é garantida a ordem de invocação
Console.WriteLine("Invoking delegate d:");
d("D"); // Ex A1 - Invocação // Ex A2 - Não é garantida a ordem de invocação
// Ex A6
Console.WriteLine("Invoking method delegate:");
callDelegate(a);
Console.Read();
}
示例2: btn_asyncTest_Click
private void btn_asyncTest_Click(object sender, EventArgs e)
{
//Assign a function to the delegate
myAsync = new MyDelegate(this.SomeFunction);
myAsync.BeginInvoke(5, 10, null, null);
}
示例3: Forget
public void Forget(string eventName, MyDelegate subscriber)
{
if (events.Keys.Contains(eventName))
{
events[eventName] -= subscriber;
}
}
示例4: Main
static void Main(string[] args)
{
MyClass obj = new MyClass();
MyDelegate del1 = new MyDelegate(obj.Func1);
del1 += new MyDelegate(obj.Func2);
//上面两句可以简写为以下形式:
//MyDelegate del1 = obj.Func1;
//del1 += obj.Func2;
Delegate[] ds;
ds = del1.GetInvocationList();
Console.WriteLine("del1的委托调用列表中包含{0}个方法", ds.GetLength(0));
del1(5); //先调用obj1.Func1(),再调用obj1.Func2()
MyDelegate del2 = new MyDelegate(obj.Func1);
del2 += new MyDelegate(obj.Func2);
//组合委托
Delegate mul = del1 + del2;
ds = mul.GetInvocationList();
Console.WriteLine("mul的委托调用列表中包含{0}个方法", ds.GetLength(0));
int ret = (mul as MyDelegate)(10); //获取委托调用列表最后一个方法的返回值
Console.WriteLine("ret = {0}", ret);
Console.ReadKey();
}
示例5: SystemReflectionType_ObjectGetType_Test0
public MFTestResults SystemReflectionType_ObjectGetType_Test0()
{
bool fRes = true;
///
/// Test object.GetType method for various types (including
/// reflection types and arrays of reflection types).
///
object o = (object)1;
fRes &= (o.GetType() == typeof(int));
o = (object)typeof(Type);
fRes &= o.GetType() == typeof(Type).GetType();
o = AppDomain.CurrentDomain.GetAssemblies();
fRes &= o.GetType() == typeof(Assembly[]);
o = new TestClass();
fRes &= o.GetType() == typeof(TestClass);
o = new TestStruct();
fRes &= o.GetType() == typeof(TestStruct);
o = new MyDelegate(MyDelegateImpl);
fRes &= o.GetType() == typeof(MyDelegate);
o = (new MyDelegate(MyDelegateImpl)).Method;
Debug.Print("object (MethodInfo) GetType: " + o.GetType().ToString());
MethodInfo mi = typeof(SystemReflectionTypeTests).GetMethod("MyDelegateImpl", BindingFlags.Static | BindingFlags.NonPublic);
fRes &= o.GetType() == mi.GetType();
return fRes ? MFTestResults.Pass : MFTestResults.Fail;
}
示例6: TestCombineRemove
public void TestCombineRemove()
{
MyDelegate dela = new MyDelegate( MethodA );
MyDelegate delb = new MyDelegate( MethodB );
MyDelegate delc = new MyDelegate( MethodC );
MyDelegate deld = new MyDelegate( MethodD );
string val;
char res;
// test combine
MyDelegate del1, del2;
del1 = dela + delb + delb + delc + delb + delb + deld;
val = "";
res = del1( ref val );
Assert.AreEqual("abbcbbd", val , "#A01");
Assert.AreEqual('d', res , "#A02");
// test remove
del2 = del1 - ( delb + delb );
val = "";
res = del2( ref val );
Assert.AreEqual("abbcd", val , "#A03");
Assert.AreEqual('d', res , "#A04");
// we did not affect del1, did we?
val = "";
res = del1( ref val );
Assert.AreEqual("abbcbbd", val , "#A05");
}
示例7: DelegateTimer
public DelegateTimer(DelegateTimerKey key, MyDelegate d, float remainTime)
{
mTimerKey = key;
mDelegate = d;
mTime = remainTime;
mRemainTime = remainTime;
}
示例8: Main
public static void Main()
{
MyDelegate a, b, c, d;
// Create the delegate object a that references
// the method Hello:
a = new MyDelegate(Hello);
// Create the delegate object b that references
// the method Goodbye:
b = new MyDelegate(Goodbye);
// The two delegates, a and b, are composed to form c:
c = a + b;
// Remove a from the composed delegate, leaving d,
// which calls only the method Goodbye:
d = c - a;
Console.WriteLine("Invoking delegate a:");
a("A");
Console.WriteLine("Invoking delegate b:");
b("B");
Console.WriteLine("Invoking delegate c:");
c("C");
Console.WriteLine("Invoking delegate d:");
d("D");
}
示例9: Main
static void Main(string[] args)
{
MyDelegate arithmethod = null;
Console.WriteLine("Please insert two integer numbers");
int input1 = Convert.ToInt32(Console.ReadLine());
int input2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please choose your operation as + , - , * , M");
char operationinput = Convert.ToChar(Console.ReadLine());
switch (operationinput)
{
case '+':
arithmethod = new MyDelegate(Add);
break;
case '-':
arithmethod = new MyDelegate(Subtrac);
break;
case '*':
arithmethod = new MyDelegate(Multiply);
break;
case 'M':
arithmethod = new MyDelegate(Max);
break;
}
int r = arithmethod(input1, input2);
Console.WriteLine("The result of your {0} operation is {1}", operationinput, r);
Console.ReadLine();
}
示例10: Main
static void Main(string[] args)
{
deli = new MyDelegate(MyMethod);
Console.WriteLine("Invoking the method...");
result = deli.BeginInvoke(MyMethodEnds, null);
Console.WriteLine("Reached Console.ReadKey()");
Console.ReadKey();
}
示例11: Main
public static void Main(){
MyDelegate call = new MyDelegate(FirstMethod);
call += new MyDelegate(SecondMethod);
call("Message A");
call("Message B");
call("Message C");
}
示例12: Run
public void Run()
{
MyStruct s = new MyStruct();
s.b = "OK";
MyDelegate dg = new MyDelegate(s.Test);
dg();
}
示例13: neuDelegate
public MyDelegate neuDelegate()
{
Class1 c2 = new Class1();
MyDelegate d1 = new MyDelegate(c2.delegateMethod1);
MyDelegate d2 = new MyDelegate(c2.delegateMethod2);
MyDelegate d3 = d1 + d2;
return d3;
}
示例14: MyDelegate
delegate string MyDelegate();//声明委托
static void Main(string[] args)
{
Helloworld hello = new Helloworld();
MyDelegate h = new MyDelegate(hello.HelloCN);
Console.WriteLine(h());
h = new MyDelegate(hello.HelloEN);
Console.WriteLine(h());
}
示例15: Subscribe
public void Subscribe(string eventName, MyDelegate subscriber)
{
if (!events.Keys.Contains(eventName))
{
events.Add(eventName, null);
}
events[eventName] += subscriber;
}