I have a generic class defined like this:
public abstract class StationProperty{}
public class StationProperty<T>
{
public int Id { get; set; }
public T Value { get; set; }
}
I then have a list that looks like this:
var props = new List<StationProperty>();
So far so good, except when I try to add an item to the list like this:
var prop = new StationProperty<bool>(39, true);
// var prop = new StationProperty<int>(39, 100); another example
props.Add(prop);
It throws a design time error of: Argument type < bool> is not assignable to parameter type X
My idea is to have a list of values which are strongly typed (not using object), then be able to infer their types using generics later down the road.
That's because a StationProperty<T>
isn't a StationProperty
. The only thing they have in common is a name. Perhaps one should derive from the other?
public class StationProperty<T>:StationProperty{}
StationProperty
and StationProperty<T>
are two independent classes.
You're creating List of first and inserting there instance of second - that can't be done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.