c# - How to make a List of Generic Objects? -
i'm having trouble situation:
i've generic class:
public class informationsolv<t> { private t _valeur; public string nom { get; private set; } public datetime timestamp { get; set; } ... }
i store objects of informationsolv type in collection.
so store in list of objects have cast every time want information object.
so thought "i should make abstract class , make informationsolv inherit it"
so did:
public abstract class ainformationsolv { public string nom; public datetime timestamp; }
and changed informationsolv this:
public class informationsolv<t>:ainformationsolv { private readonly t _valeur; ... }
it's not bad, if make list of ainformationsolv objects , store informationsolv objects in it. can access nom , timestamp variables (without cast) of course can't access _valeur neither can put _valeur in ainformationsolv class, because have become generic class , problem same.
is there way can make in abstract class:
public abstract class ainformationsolv { public string nom; public datetime timestamp; protected ? _valeur; }
where ? means: "child classes contain _valeur class variable, don't know type variable, child define it".
thank you
is there way can define member type mans "children classes contain _valeur class variable, don't know type variable, children define it".
sure - use object
type:
protected object _valeur;
think - if _valeur
can of different types - how know type you're dealing @ compile time? , how use it?
take list<t>
example - suppose want list<list<?>>
, inner list can of type. right can use list<object>
, can use list<ienumerable<object>>
guarantee inner lists @ least form of ienumerable<t>
. let's suppose syntax existed:
var funkylist = new list<list<?>>();
i can add lists of type:
funkylist.add(new list<int>()); funkylist.add(new list<foo>()); funkylist.add(new list<bar>());
now extract 1 of inner list
s:
list<?> l = funkylist[1];
but ?
? if don't know type of list is, how going useful it? @ best you'd have use reflection (or dynamic
) inner type anyways, negates power of generics.
Comments
Post a Comment