JL.
JL.

Reputation: 81262

How to get to the properties of a generic class in a list?

I have a generic class like this:

public class StationProperty<T> : StationProperty
   {
      public StationProperty()
      {

      }

      public StationProperty(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; }
   }

Notice the inheritance, this I'll explain later but the abstract class looks like this:

 public interface StationProperty
   {

   }

As you can see nothing fancy - and no explicit properties.

Thanks to this mechanism I can pass around a List of these items like this:

var props = new List<StationProperty>();
props.Add(new StationProperty<bool>(39, true));
props.Add(new StationProperty<int>(41, 1));

So far this is all going smooth, but now I would expect to be able to do a :

Foreach(var prop in props)
{
     //prop.Id
     //prop.Desc
     //and most importantly prop.Value.GetType or prop.GetType
}

Instead these properties are missing:

enter image description here

If I manually add the properties to the abstract class then I can solve Id and Desc, but I would most likely need to add an object type for Value, and this would negate the reason for using generics in the first place.

So my question is, Can what I want be done? And where am I going wrong.

Upvotes: 0

Views: 102

Answers (2)

Fried Hoeben
Fried Hoeben

Reputation: 3272

Are you looking for code like below? You can always get the type but only read the value as 'object' when you are using the interface, the generic class can also get you the strongly typed value and allow you to set. You could also allow Value to be set via the interface and throw an exception if it is not the right type.

public class StationProperty<T> : StationProperty
{
    public StationProperty()
    {
    }

    public StationProperty(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 StationProperty.Value
    {
        get { return Value; }
    }

    public Type ValueType
    {
        get { return typeof (T); }
    }
}

public interface StationProperty
{
    int Id { get; set; }
    string Desc { get; set; }
    object Value { get; }
    Type ValueType { get; }
}

Upvotes: 3

raznagul
raznagul

Reputation: 375

For the Id and Desc properties the easiest way to get them, is to add them to the interface.

Or in you example it might even be better to use an (abstract) class instead of the interface and put the properties there. With this approach you don't have to implement them in your generic class.

To get the Value property in the loop you need to use reflections:

var val = prop.GetType().GetProperty("Value").GetValue(prop);

This statement should do the trick.

Although, as @Steve Mitcham said in the comments, if you describe you original problem, maybe there is an even better solution.

Upvotes: 0

Related Questions