函数的动态返回类型 [英] dynamic return type of a function

查看:108
本文介绍了函数的动态返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能创建一个将根据参数类型有一个动态的返回类型的功能?



如同

 保护DynamicType测试(DynamicType型)
{

的回报;

}


解决方案

您' d必须使用泛型这一点。例如,

 保护T测试< T>(T参数)
{

}

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

  INT富; 

INT吧=测试与LT; INT>(富);


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

Like

protected DynamicType Test(DynamicType type)
{

return ; 

}

解决方案

You'd have to use generics for this. For example,

protected T Test<T>(T parameter)
{

}

In this example, the '<T>' tells the compiler that it represents the name of a type, but you don't know what that is in the context of creating this function. So you'd end up calling it like...

int foo;

int bar = Test<int>(foo);

这篇关于函数的动态返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆