我有两个功能:
double fullFingerPrinting(string location1, string location2, int nGrams)
double AllSubstrings(string location1, string location2, int nGrams)
我想进入循环并依次激活每个函数,并且在每个函数之后我还要打印函数的名称,我该怎么做?
答案 0 :(得分:5)
Delegate.Method
属性获取方法名称。示例(编辑以显示非静态函数代理):
class Program
{
delegate double CommonDelegate(string location1, string location2, int nGrams);
static void Main(string[] args)
{
SomeClass foo = new SomeClass();
var functions = new CommonDelegate[]
{
AllSubstrings,
FullFingerPrinting,
foo.OtherMethod
};
foreach (var function in functions)
{
Console.WriteLine("{0} returned {1}",
function.Method.Name,
function("foo", "bar", 42)
);
}
}
static double AllSubstrings(string location1, string location2, int nGrams)
{
// ...
return 1.0;
}
static double FullFingerPrinting(string location1, string location2, int nGrams)
{
// ...
return 2.0;
}
}
class SomeClass
{
public double OtherMethod(string location1, string location2, int nGrams)
{
// ...
return 3.0;
}
}
答案 1 :(得分:0)
不确定这是否有帮助,但我做了一个帖子delegates and events that fire,您可以使用委托并将其附加到一个事件中,如果您调用该事件,它将触发附加到该事件的所有代理,所以不需要循环。