C#中的通用方法的类型约束 [英] Type Constrain For Generic Method in C#
本文介绍了C#中的通用方法的类型约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请查看此代码块:
Please have a look at this code block:
using Local = Local.A
using Global = Global.A
public void TreeViewBinding<T>(T t) where T : Local
where T : Global
{
//My code goes here
}
我知道这段代码不起作用。所以我的问题是:
有没有其他方法来存档这个方法?鉴于两个类A不在同一个命名空间中,我也注意到他们甚至没有实现任何通用接口。
目前,这是我的解决方案。这些都不方便:
I know this code does not work. So my question is :
" Is there any other ways to archive this method? Given that the two classes A do not reside in the same namespace and I also note that they even do not have implement any common Interface."
At the moment, here's my solution. these aren't convenience ways:
public void TreeViewBinding(Local localParam)
{
//My code goes here
DoAnotherTask(localParam)
}
public void TreeViewBinding(Global globalParam)
{
//My code goes here
DoAnotherTask(globalParam)
}
推荐答案
是你确定你理解对泛型约束的预期用途吗?全局和本地类型(类)?
Are you sure you understand the intended use for constraints on generics? Are Global and Local types (classes)?
这篇关于C#中的通用方法的类型约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文