dynamic return type of a function

后端 未结 5 1026
春和景丽
春和景丽 2021-02-07 04:46

How can I create a function that will have a dynamic return type based on the parameter type?

Like

protected DynamicType Test(DynamicType type)
{

retur         


        
5条回答
  •  走了就别回头了
    2021-02-07 05:11

    C# is not a dynamic language. To tackle this problem in C# you can return a generic object and typecast later to whatever you think the value should be -- not recommended. You can also return an interface, this way you don't really care about a specific class instance. As others have pointed out you can also use generics. It really depends on what you need / want to do inside the body of the function since all the methods above have their own limitations.

提交回复
热议问题