您是否对通用类型V施加约束?您需要告诉运行时V可以是您的Item
类型的子类型的任何类型.
public class MyGenericClasswhere V : Item //This is a constraint that requires type V to be an Item (or subtype) { public void DoSomething() { List myList = someMethod(); foreach (V element in myList) { //This will now work because you've constrained the generic type V Guid test = element.IetmGuid; } } }
注意,如果您需要支持多种项目(由Item的子类型表示),则以这种方式使用泛型类是有意义的.
您是否对通用类型V施加约束?您需要告诉运行时V可以是您的Item
类型的子类型的任何类型.
public class MyGenericClasswhere V : Item //This is a constraint that requires type V to be an Item (or subtype) { public void DoSomething() { List myList = someMethod(); foreach (V element in myList) { //This will now work because you've constrained the generic type V Guid test = element.IetmGuid; } } }
注意,如果您需要支持多种项目(由Item的子类型表示),则以这种方式使用泛型类是有意义的.