c# - NET4 features: should casting generic IEnumerable to IEnumerable be possible? -



c# - NET4 features: should casting generic IEnumerable<T> to IEnumerable<MoreSpecificT> be possible? -

if have internal method in .net 4 library (running on mono here):

protected internal ienumerable<kscomponent> getcomponentsbytype(type componenttype) { homecoming this.componentsdic [componenttype]; }

and expose generic parameter simple usage. have done far , working:

public ienumerable<tcomponenttype> getcomponentsbytype<tcomponenttype>() tcomponenttype : kscomponent { homecoming this.getcomponentsbytype (typeof(tcomponenttype)).cast<tcomponenttype>(); }

however, shouldn't .net 4.0 work:

public ienumerable<tcomponenttype> getcomponentsbytype<tcomponenttype>() tcomponenttype : kscomponent { homecoming (ienumerable<tcomponenttype>)this.getcomponentsbytype (typeof(tcomponenttype)); }

edit:

kscomponent base of operations class, tcomponenttype can (e.g.) ksmovecomponent, subclasses kscomponent. situation have class code above holds list of kscomponent specific type, instance:

typeof(ksmovecomponent) maps list<kscomponent>(). elements of list of type ksmovecomponent. i'd have list<kscomponent> beingness cast list<ksmovecomponent> straight instead of using linq.cast() extension.

it's coming downwards question: have list of cars , can 100% sure mercedes - why can't list of mercedes then? :-)

covariance allows cast enumerable more generic version.

the generic status on other hand allows replace generic type more restrictive.

so why work? seems 2 of them going in 2 different directions - no?

c# .net .net-4.0 mono covariance

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -