jeudi 2 juillet 2020

How to get type of objects in an empty list of an interface

Consider:

public interface I
{
    int InterfaceProperty {get;set;}
}

public class C1 : I
{
    public int InterfaceProperty {get;set;}
    public int Class1Property {get;set;}
}

public class C2 : I
{
    public int InterfaceProperty {get;set;}
    public int Class2Property {get;set;}
}

//In some other class:
public List<I> L;
void somemethod()
{
    this.L = new List<C1>();
    SomeMethodToGetProperties(L);
    this.L = new List<C2>();
    SomeMethodToGetProperties(L);
}

I need SomeMethodToGetProperties that gets a list of the properties for C1 or C2. ie, first call returns InterfaceProperty and Class1Property and the second call returns InterfaceProperty and Class2Property.

I can't use an object in the list, because the lists may be empty. I tried Reflection on the lists, but that only gave me the properties for the interface.





Aucun commentaire:

Enregistrer un commentaire