C# 如何将泛型类型参数传递给从构造函数调用的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15191888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass a generic type parameter to a method called from a constructor?
提问by Phil
Here is the constructor:
这是构造函数:
public PartyRoleRelationship(PartyRole firstRole, PartyRole secondRole)
{
if (firstRole == secondRole)
throw new Exception("PartyRoleRelationship cannot relate a single role to itself.");
if (firstRole.OccupiedBy == null || secondRole.OccupiedBy == null)
throw new Exception("One or both of the PartyRole parameters is not occupied by a party.");
// Connect this relationship with the two roles.
_FirstRole = firstRole;
_SecondRole = secondRole;
T = _FirstRole.GetType().MakeGenericType();
_SecondRole.ProvisionRelationship<T>(_FirstRole); // Connect second role to this relationship.
}
On the last line, where it calls ProvisionRelationship on _SecondRole, it's giving me the run-time error: Type or namespace 'T' could not be found...
在最后一行,它在 _SecondRole 上调用 ProvisionRelationship,它给了我运行时错误:找不到类型或命名空间“T”...
How do I either (a) properly assign T, or (b) pass a generic type with the constructor? I've been looking through quite a few posts, but may have missed something due to a lack of understanding. Anyone's help on this would be greatly appreciated.
我如何 (a) 正确分配 T,或 (b) 使用构造函数传递泛型类型?我已经浏览了很多帖子,但由于缺乏理解可能错过了一些东西。任何人对此的帮助将不胜感激。
采纳答案by Cheesebaron
Your class needs to be generic. So PartyRoleRelationship
needs to look like this:
你的类需要是通用的。所以PartyRoleRelationship
需要看起来像这样:
public class PartyRoleRelationship<T>
{
public PartyRoleRelationship(T arg, ...)
{
}
}
Read more about generic classes here:
在此处阅读有关泛型类的更多信息:
http://msdn.microsoft.com/en-us/library/sz6zd40f(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/sz6zd40f(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
Edit:
编辑:
You could probably simplify you code a bit and do it like this:
您可能可以稍微简化一下代码并这样做:
public class RoleRelationship<T>
{
public RoleRelationship(T firstRole, T secondRole)
{
if (firstRole.OccupiedBy == null || secondRole.OccupiedBy == null)
throw new Exception("One or both of the Role parameters is not occupied by a party.");
// Connect this relationship with the two roles.
_FirstRole = firstRole;
_SecondRole = secondRole;
_SecondRole.ProvisionRelationship<T>(_FirstRole);
}
}
回答by user1610015
If you know the type of _FirstRole statically (is it PartyRole?), you can just use that:
如果您静态地知道 _FirstRole 的类型(它是 PartyRole 吗?),您可以使用它:
_SecondRole.ProvisionRelationship<PartyRole>(_FirstRole);
回答by Moop
Make a generic class where the generic type T is a type of the base class PartyRole:
创建一个泛型类,其中泛型类型 T 是基类 PartyRole 的一种类型:
public class PartyRoleRelationship<T> where T : PartyRole
{
T _FirstRole;
T _SecondRole;
public PartyRoleRelationship(T role1, T role2) {
_FirstRole = role1;
_SecondRole = role2;
role1.ProvisionRelationship(role2)
}
public ProvisionRelationship(T otherRole) {
// Do whatever you want here
}
}