dynamic return type of a function

后端 未结 5 1027
春和景丽
春和景丽 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:09

    Whilst the accepted answer is good, it has been over two years since it was written, so, I should add that you can use:

    protected dynamic methodname(dynamic input)
    {
        return input;
    }
    

    Input will be returned as the same type, and you do not need to call the method as a generic.

    Reference:
    https://msdn.microsoft.com/en-us/library/dd264736.aspx

提交回复
热议问题