我正在尝试在子类中访问一个泛型类型的属性。在下面的示例中,我重新创建了我的问题。这个问题有解决方法吗,还是根本不可能实现?谢谢!
编辑:无法将集合声明为
编辑:无法将集合声明为
A<Model>
或 A<T>
。
public abstract class Model {
public int Id { get; }
}
public interface I<T> where T: Model {
ICollection<T> Results { get; }
}
public abstract class A { }
public class A<T> : A, I<T> where T : Model {
public ICollection<T> Results { get; }
}
public class Example {
A[] col;
void AddSomeModels() {
col = new A[] {
new A<SomeModel>(),
new A<SomeOtherModel>()
}
}
void DoSomethingWithCollection() {
foreach (var a in col) {
// a.Results is not known at this point
// is it possible to achieve this functionality?
}
}
}
AbstractModel
),但无法将集合声明为A<AbstractModel>
。已经尝试过了。 - Goos van den Bekerom