[英]return generic IEnumerable when passing concrete type parameter for a generic method
[英]Method to return a generic type when passing in a delegate as a parameter
我正在尝试“通用化”我们在系统中散布的一些代码。
我想要:
我对泛型非常陌生,因此对您的帮助表示赞赏。
下面是我的空中手指所在的位置(!)
public static T ReturnSingleObject<T>(Func<string, int, T> dynamicSignature)
{
T returnValue;
ServiceReference wCFService;
try
{
wCFService = new BusinessServiceClient();
returnValue = dynamicSignature();
//returnValue = wCFService.AMETHOD(PARAM1, PARAM2);
return returnValue;
}
catch (Exception)
{
if (wCFService != null) wCFService.Abort();
throw;
}
finally
{
if (wCFService != null) wCFService.Close();
}
}
看起来您的dynamicSignature函数缺少几个参数,因此您需要添加这些参数。 同样,您可以将return语句移至底部,而不是try块中,并将返回值初始化为默认值:
T returnValue = default(T);
...
try
{
...
returnValue = dynamicSignature(somestring, someint);
...
}
...
return returnValue;
您应该查看该链接,该链接涉及声明通用委托: http : //msdn.microsoft.com/zh-cn/library/sx2bwtw7.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.