0
Я сделал простой пример делегата, чтобы попытаться понять делегатов. Вот код:Свойство метода делегата
namespace DelegateExample
{
public delegate int BinaryOp(int x, int y);
public class SimpleMath
{
public static int Add(int x, int y) { return x + y; }
public static int Subtract(int x, int y) { return x - y; }
public static int Multiply(int x, int y) { return x * y; }
public static int Divide(int x, int y) { return x/y; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*******Simple Delegate Example************");
BinaryOp d = new BinaryOp(SimpleMath.Multiply);
d += SimpleMath.Divide;
d += SimpleMath.Add;
Display(d);
Console.ReadLine();
}
public static void Display(Delegate dobj)
{
foreach (BinaryOp del in dobj.GetInvocationList())
{
int ans = del.Invoke(10, 10);
Console.WriteLine(ans);
Console.WriteLine("Method Name: {0}", dobj.Method);
}
Console.WriteLine("+++++++++++++++++++++++++++++++++++");
}
}
}
и здесь выход:
*******Simple Delegate Example*********
100
Method Name: Int32 Add(Int32, Int32)
1
Method Name: Int32 Add(Int32, Int32)
20
Method Name: Int32 Add(Int32, Int32)
+++++++++++++++++++++++++++++++++++
Мой вопрос: В выводе, почему свойство .method возвращают то же имя («Добавить» в каждом конкретном случае), но верный результат - вызов Multiply, Divide then Add?