C# collections type constrained generics -



C# collections type constrained generics -

i trying in c# pretty straightforward in java, using wildcard type-bounding. i've tried boil downwards java code necessary illustrate problem (compiles):

public abstract class parentclass { public abstract somebindinglist<? extends parentclass> parentlist(); } public class childclass extends parentclass { private somebindinglist<childclass> mylist; public childclass() { // load from/bind database mylist = new somebindinglist<childclass>(); } public somebindinglist<? extends parentclass> parentlist() { homecoming mylist; } }

perhaps need emphasize next line since marked duplicate: somebindinglist 3rd-party bindinglist can't alter it. parameterized , can't replaced non-parameterized version.

the problem, of course, how implement parentlist() method in childclass homecoming list can used list of parentclass objects.

it seems there should way utilize where keyword provide constrained type in c#, can't create work (syntactically @ least) class extends class, i.e. childclass, , there doesn't seem way parameterize homecoming value of method (or property).

i create new list , set childclass items in new list parentclass items, (aside beingness kludgy) afraid interfere behavior of somebindinglist.

i'm no c# expert, i'm sure more familiar language knows answer. thanks!

@codecaster -- i've tried many variations of c# code (this not compile, , can't find variation does):

public abstract class parentclass { public abstract list<t> parentlist<t>() t : parentclass; } public class childclass { public list<childclass> mylist; public childclass() { mylist = new list<childclass>(); } public override list<t> parentlist<t>() t : parentclass { homecoming mylist; } }

i've tried parameterizing childclass<t> causes generation of list<childclass<t>> not list<t>

i think issue here related desired co-variance of relationship between inheritance hierarchy of generic 3rd-party somebindinglist<t> class , hierarchy of types used parameters.

first, allow me give code compile:

public interface isomebindinglist<out t> { } public abstract class parentclass { public abstract isomebindinglist<parentclass> parentlist(); } public class childclass : parentclass { private isomebindinglist<childclass> mylist; public childclass() { // load from/bind database // mylist = new somebindinglist<childclass>(); // <-- need figure out } public override isomebindinglist<parentclass> parentlist() { homecoming mylist; } }

c# not provide generic type co-variance classes. interfaces. have think outside box , implement trivial adapter 3rd-party somebindinglist<t>, implements lifted members co-variantly compatible, is: members t occurs output.

for example, assuming somebindinglist<t> contains method t get(), lift fellow member adapter interface, , create trivial adapter implementation.

this finish code:

public interface isomebindinglist<out t> { t get(); } public class somebindinglistadapter<t> : somebindinglist<t>, isomebindinglist<t> { } public abstract class parentclass { public abstract isomebindinglist<parentclass> parentlist(); } public class childclass : parentclass { private isomebindinglist<childclass> mylist; public childclass() { // load from/bind database mylist = new somebindinglistadapter<childclass>(); } public override isomebindinglist<parentclass> parentlist() { homecoming mylist; } }

c# generics collections covariance type-constraints

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -