문제

Basically I have a method with the following signature:

public void Save(BindingList<T> items);

and I'm trying to call it using

classInstance.Save(items); //items = BindingList<ObjectInheritedFromT>

but it seems that C# doesn't realize the inheritance throwing a compile-time error, where all code written for T would be completely functional for ObjectInheritedFromT as well, wouldn't it?.

Is there a work around for this? I realized this works if it were IEnumerable, but then (oddly enough) it turns out BindingList<> doesn't implement IEnumerable, and I need to perform operations such as Remove and Add.

도움이 되었습니까?

해결책

Because BindingList<T> isn't declared as BindingList<out T> - it doesn't support covariance

Give this a read:

Covariance and Contravariance FAQ

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top