vendredi 9 septembre 2016

C# downcast to a class with generics

I have to following classes:

public class RSourceBase{}
public class RSource<T> : RSourceBase{
   public Dictionary<long,T> FetchedData {get;set;}{}
}
public class RSourceFoo : RSource<Foo>{}
public class RSourceFoo2 : RSource<Foo2>{}
public class RSourceFoo3 : RSource<Foo3>{}
etc.

I have many classes like RSourceFoo, with all kinds of objects as T.

Now I have this function:

public void FetchDataFromSources(List<RSourceBase> sources)
{
   foreach (RSourceBase src in sources) //src is actually of type RSource<>
   {
       var data = src.GetType().GetProperty("FetchedData").GetValue(src);
       //Now I want to go over the Values of FetchedData: data.Values
   }
}

So I want to go over the values of the dictionary which uses generics (in order to use reflection and get a certain property of each value which is of type T, but this is irrelevant to the question)

Does somebody know how to do it? Thanks





Aucun commentaire:

Enregistrer un commentaire