这编译:
This compiles:
class ReplicatedBaseType
{
}
class NewType: ReplicatedBaseType
{
}
class Document
{
ReplicatedBaseType BaseObject;
Document()
{
BaseObject = new NewType();
}
}
但这并不:
class DalBase<T> : where T: ReplicatedBaseType
{
}
class DocumentTemplate
{
DalBase<ReplicatedBaseType> BaseCollection;
DocumentTemplate ()
{
BaseCollection= new DalBase<NewType>(); // Error in this line. It seems this is not possible
}
}
是什么原因?
What's the reason?
差异存在于C#4.0打靶.NET 4中),但在限于接口和的使用/
退出
(哦,和引用类型的数组)。例如,为了使一个协变序列:
Variance exists in C# 4.0 targetting .NET 4), but is limited to interfaces and usage of in
/out
(oh, and arrays of reference-types). For example, to make a covariant sequence:
class DalBase<T> : IEnumerable<T> where T: ReplicatedBaseType
{
public IEnumerator<T> GetEnumerator() {throw new NotImplementedException();}
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
}
class DocumentTemplate
{
IEnumerable<ReplicatedBaseType> BaseCollection;
DocumentTemplate()
{
BaseCollection = new DalBase<NewType>(); // Error in this line. It seems this is not possible
}
}
但除此之外...没有。坚持要么非通用列表(的IList
),或使用预期的列表类型。
But other than that... no. Stick to either non-generic lists (IList
), or use the expected list type.
上一篇:在.NET中获取的纯文本从HTML文本、NET、HTML
下一篇:EqualityComparer&LT; T&GT; .DEFAULT与T.EqualsLT、EqualityComparer、GT、Equals