gpt4 book ai didi

c# - 函数的动态返回类型

转载 作者:可可西里 更新时间:2023-11-01 07:59:40 25 4
gpt4 key购买 nike

如何创建一个具有基于参数类型的动态返回类型的函数?

喜欢

protected DynamicType Test(DynamicType type)
{

return ;

}

最佳答案

你必须为此使用泛型。例如,

protected T Test<T>(T parameter)
{

}

在此示例中,“<T> ' 告诉编译器它代表一个类型的名称,但您不知道在创建此函数的上下文中它是什么。所以你最终会这样调用它......

int foo;

int bar = Test<int>(foo);

关于c# - 函数的动态返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/744401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com