求 C# 多播委托带返回值的简单例子 百度上的都不行 最好是有高手现遍一个 高分求助 ~~
3个回答
2013-04-08
展开全部
class Program
{
//委托返回值==我们输入的msg值
public delegate string deleget_function(string msg);
//fake几个函数
private string p1_string(string msg){ System.Console.WriteLine(msg); return msg;}
private string p2_string(string msg){ System.Console.WriteLine(msg);return msg;}
private string p3_string(string msg){ System.Console.WriteLine(msg);return msg;}
private string p4_string(string msg){ System.Console.WriteLine(msg);return msg;}
public static void Main(string[] args)
{
System.Collections.Generic.IList strList = new System.Collections.Generic.List();
Program p = new Program ();
deleget_function p1 = p.p1_string;
deleget_function p2 = p.p2_string;
deleget_function p3 = p.p3_string;
deleget_function p4 = p.p4_string;
deleget_function muldelegateF = p1 + p2 + p3 + p4 ;
muldelegateF("sophy");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
{
//委托返回值==我们输入的msg值
public delegate string deleget_function(string msg);
//fake几个函数
private string p1_string(string msg){ System.Console.WriteLine(msg); return msg;}
private string p2_string(string msg){ System.Console.WriteLine(msg);return msg;}
private string p3_string(string msg){ System.Console.WriteLine(msg);return msg;}
private string p4_string(string msg){ System.Console.WriteLine(msg);return msg;}
public static void Main(string[] args)
{
System.Collections.Generic.IList strList = new System.Collections.Generic.List();
Program p = new Program ();
deleget_function p1 = p.p1_string;
deleget_function p2 = p.p2_string;
deleget_function p3 = p.p3_string;
deleget_function p4 = p.p4_string;
deleget_function muldelegateF = p1 + p2 + p3 + p4 ;
muldelegateF("sophy");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
.....多播委托..
我还以为楼主什么意思呢.我就自己写了一段.不知道符合不?
达到了获取指定方法的指定返回值..
我觉得所有需求都够了..你觉得呢?楼主不够继续问啊.
调用
msg ms = new msg(messges.m2);
ms = ms + messges.m1;
ms += messges.m3;
Delegate[] asd = ms.GetInvocationList();
for (int i = 0; i < asd.Length; i++)
{
MessageBox.Show(asd[i].Method.Name + "方法的调用返回是:" + asd[i].DynamicInvoke());
}
定义
public delegate string msg();
class messges
{
public static string m1()
{
return "方法1被调用";
}
public static string m2()
{
return "方法2被调用";
}
public static string m3()
{
return "方法3被调用";
}
}
我还以为楼主什么意思呢.我就自己写了一段.不知道符合不?
达到了获取指定方法的指定返回值..
我觉得所有需求都够了..你觉得呢?楼主不够继续问啊.
调用
msg ms = new msg(messges.m2);
ms = ms + messges.m1;
ms += messges.m3;
Delegate[] asd = ms.GetInvocationList();
for (int i = 0; i < asd.Length; i++)
{
MessageBox.Show(asd[i].Method.Name + "方法的调用返回是:" + asd[i].DynamicInvoke());
}
定义
public delegate string msg();
class messges
{
public static string m1()
{
return "方法1被调用";
}
public static string m2()
{
return "方法2被调用";
}
public static string m3()
{
return "方法3被调用";
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试看下面的代码。 说好的分呢?
delegate int DelegateFunction(int a, int b);
class Program
{
static void Main(string[] args)
{
DelegateFunction test = new DelegateFunction(Add);
test += Minus;
foreach (DelegateFunction func in test.GetInvocationList())
{
Console.WriteLine(func(2, 1));
}
}
static int Add(int x, int y)
{
Console.Write("X + Y:");
return x + y;
}
static int Minus(int x, int y)
{
Console.Write("X - Y:");
return x - y;
}
}
delegate int DelegateFunction(int a, int b);
class Program
{
static void Main(string[] args)
{
DelegateFunction test = new DelegateFunction(Add);
test += Minus;
foreach (DelegateFunction func in test.GetInvocationList())
{
Console.WriteLine(func(2, 1));
}
}
static int Add(int x, int y)
{
Console.Write("X + Y:");
return x + y;
}
static int Minus(int x, int y)
{
Console.Write("X - Y:");
return x - y;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询