Reputation: 81262
My class structure looks like this:
public interface IStationProperty
{
int Id { get; set; }
string Desc { get; set; }
object Value { get; }
Type ValueType { get; }
}
[Serializable]
public class StationProp<T> : IStationProperty
{
public StationProp()
{
}
public StationProp(int id, T val, string desc = "")
{
Id = id;
Desc = desc;
Value = val;
}
public int Id { get; set; }
public string Desc { get; set; }
public T Value { get; set; }
object IStationProperty.Value
{
get { return Value; }
}
public Type ValueType
{
get { return typeof(T); }
}
}
Which allows me to add multiple generic types to the same list like this:
var props = new List<IStationProperty>();
props.Add(new StationProp<int>(50, -1, "Blah"));
props.Add(new StationProp<bool>(53, true, "Blah"));
props.Add(new StationProp<int>(54, 10, "Blah"));
What I would like to do now is be able to change just the value of an item in this list, without changing the Type.
Is this possible?
Upvotes: 0
Views: 348
Reputation: 12616
I assume that you know an index of the item you want to change and you know what type it is. Then it goes like the following
(props[0] as StationProp<int>).Value = 5;
If you are not sure of its type
var item = props[i] as StationProp<int>;
if (item != null)
{
item.Value = 5;
}
Does this answer your question? I'm not much sure what else would you like to achieve.
Upvotes: 2