C# 具有不同retun类型的泛型委托

C# 具有不同retun类型的泛型委托,c#,delegates,C#,Delegates,我有一些不同返回类型的方法,比如int和double 但我现在尝试迭代不同的方法 我试着这样做: delegate T MeDelegate<T>(); class Program { static void Main(string[] args) { Func<int> d = ReturnFive; d += ReturnTen; d += Retu

我有一些不同返回类型的方法,比如int和double

但我现在尝试迭代不同的方法

我试着这样做:

delegate T MeDelegate<T>();
    class Program
    {
        static void Main(string[] args)
        {
            Func<int> d = ReturnFive;
            d += ReturnTen;
            d += ReturnTwentyTwo;
            d += returnDoubles;

            foreach (int i in GetAllReturnValues(d))
                Console.WriteLine(i);
            Console.ReadKey();


        }

        static IEnumerable<TArgs> GetAllReturnValues<TArgs>(Func<TArgs> d)
        {

            List<int> ints = new List<int>();
            foreach (Func<TArgs> item in d.GetInvocationList())
                yield return item();


        }

        static int ReturnFive() { return 5; }
        static int ReturnTen() { return 10; }
        static int ReturnTwentyTwo() { return 22; }
        static double returnDoubles() { return 1.0; }

    }
委托T MeDelegate();
班级计划
{
静态void Main(字符串[]参数)
{
Func d=5;
d+=返回十;
d+=返回值22;
d+=双打;
foreach(GetAllReturnValues(d)中的int i)
控制台写入线(i);
Console.ReadKey();
}
静态IEnumerable GetAllReturnValues(函数d)
{
List ints=新列表();
foreach(d.GetInvocationList()中的Func项)
收益返回项();
}
静态int ReturnFive(){return 5;}
静态int ReturnTen(){return 10;}
静态int ReturnTwentyTwo(){return 22;}
静态双returnDoubles(){return 1.0;}
}
当然了:
Func d

不起作用。但如何改善这一点呢


谢谢

我不确定这是否是您想要的,但请尝试使用下面的代码

delegate T MeDelegate<T>();
class Program
{
    static void Main(string[] args)
    {
        Func<object> functionDelegates = ReturnFive;
        functionDelegates += ReturnTen;
        functionDelegates += ReturnTwentyTwo;
        functionDelegates += ReturnSomeDoubleValue;

        foreach (object value in GetAllReturnValues(functionDelegates))
        {
            Console.WriteLine(value);
        }
        Console.ReadKey();
    }

    static IEnumerable<TArgs> GetAllReturnValues<TArgs>(Func<TArgs> functionDelegates)
    {
        return from Func<TArgs> function in functionDelegates.GetInvocationList() select function();
    }

    static object ReturnFive() { return 5; }
    static object ReturnTen() { return 10; }
    static object ReturnTwentyTwo() { return 22; }
    static object ReturnSomeDoubleValue() { return 1.0; }

}
委托T MeDelegate();
班级计划
{
静态void Main(字符串[]参数)
{
Func functionDelegates=ReturnFive;
functionDelegates+=ReturnTen;
functionDelegates+=返回二十二;
functionDelegates+=ReturnSomeDoubleValue;
foreach(GetAllReturnValues(functionDelegates)中的对象值)
{
控制台写入线(值);
}
Console.ReadKey();
}
静态IEnumerable GetAllReturnValues(Func functionDelegates)
{
从functionDelegates.GetInvocationList()选择函数()中的Func函数返回;
}
静态对象ReturnFive(){return 5;}
静态对象ReturnTen(){return 10;}
静态对象ReturnTwentyTwo(){return 22;}
静态对象ReturnSomeDoubleValue(){return 1.0;}
}

+=连接事件。您要做的是将它们存储在一个数组中

class Program
    {
        static void Main(string[] args)
        {
            Func<object>[] d = new Func<object>[] { ReturnFive, ReturnTen, ReturnTwentyTwo, returnDoubles };

            foreach (object i in GetAllReturnValues<object>(d))
                Console.WriteLine(i);
            Console.ReadKey();


        }

        static IEnumerable<object> GetAllReturnValues<TArgs>(Func<object>[] d)
        {

            List<int> ints = new List<int>();
            foreach (Func<object> item in d)
                yield return item();


        }

        static object ReturnFive() { return 5; }
        static object ReturnTen() { return 10; }
        static object ReturnTwentyTwo() { return 22; }
        static object returnDoubles() { return 1.0; }

    }
类程序
{
静态void Main(字符串[]参数)
{
Func[]d=新的Func[]{ReturnFive,ReturnTen,returntwent22,returnDoubles};
foreach(GetAllReturnValues(d)中的对象i)
控制台写入线(i);
Console.ReadKey();
}
静态IEnumerable GetAllReturnValues(Func[]d)
{
List ints=新列表();
foreach(d中的Func项)
收益返回项();
}
静态对象ReturnFive(){return 5;}
静态对象ReturnTen(){return 10;}
静态对象ReturnTwentyTwo(){return 22;}
静态对象returndouble(){return 1.0;}
}

不确定您想要实现什么,但是如果您想要具有不同返回类型的函数,您可以随时使用动态

List<dynamic> allReturns = new List<dynamic>{ ReturnFive(), ReturnTen(), ReturnTwentyTwo(), returnDoubles() };

 foreach (dynamic i in allReturns)
      Console.WriteLine(i);
  Console.ReadKey();
List allReturns=新列表{ReturnFive(),ReturnTen(),returntwenttwo(),returnDoubles()};
foreach(allReturns中的动态i)
控制台写入线(i);
Console.ReadKey();

你不能。func的类型是在编译时设置的,它不是双精度的。不确定您在这里尝试什么,但是如果您必须这样做,只需返回一个封装返回值的类型并完全删除泛型即可。您只是将事件附加到d。你想用指向函数的指针填充d。func[]d=新的func[]{ReturnFive,ReturnTen,returntwent22,returnDoubles}@SavantCodeEngineer没有任何理由不使用
dynamic
@AmitPore没有理由使用
dynamic
。您实际上没有在此处使用
dynamic
的任何功能。如果您使用
object
,这段代码实际上也会做同样的事情,只是它会运行得更快,因为所有内容都可以静态绑定。@Servy感谢我理解,为什么要在一行中使用“dynamic”或“object”编写委托?这是一个示例。这只是为了证明问题。据推测,在实际代码中,有一个很好的理由,就是这些方法不能立即调用一次。也许他们需要调用代码提供的参数,也许他们被多次调用,等等。使用委托有很多原因,将所有这些都包含在问题中可能会分散对所问问题的注意力。OP使用
+=
没有错。C#中的所有委托都是多播委托,因此可以像OP那样进行组合。我写这篇文章时没有意识到这一点,但在回家的路上,我开始考虑+=和调用ALLIN职业,并意识到我可能是个白痴。谢谢你的确认。