i wondering whether there way limit type variable class only. example
public class product { public string name; public bool install; public list<type> components; public list<type> prerequisite; }
if list can specified of typeof(product) , derivatives, think make generic design more straightforward , easy use. maybe so:
public class product { public string name; public bool install; public list<type<product>> components; public list<type<product>> prerequisite; }
do know of method so? thank thoughts.
now, question seems strange me. sure don't have list of product
s components instead of type
s? regardless, here's way it:
class wheel : product { } class engine : product { } class product { list<type> components; public ireadonlycollection<type> components { { return components } } void addcomponent<t>() t : product { components.add(typeof(t)); } } var car = new product(); car.addcomponent<engine>(); car.addcomponent<wheel>();
so can register type known caller @ compile time , add list of types. not allow adding other types since components
property read , not allow modifying resulting collection.
are sure don't want product
's components
products well?
in case can use standard polymorphism (specifically, subtype polymorphism):
product car = new product(); car.components.add(new engine()); car.components.add(new wheel()); car.components.add(new wheel()); car.components.add(new wheel()); car.components.add(new wheel());
Comments
Post a Comment