jeudi 15 novembre 2018

how to know a type is directly implement an interface?

I have three interfaces:

interface ILocalData
{
    int Pkey { get; set; }
}

interface IRegionData : ILocalData
{
    int RegionServerPkey { get; set; }
}

interface IMainData : IRegionData
{
    int MainServerPkey { get; set; }
}

and some types inherited from them:

//data in local database only
class Type1 : ILocalData
{
    public int Pkey { get; set; }

    public string Data1 { get; set; }
}

//data in local database and region server database
class Type2 : IRegionData
{
    public int Pkey { get; set; }
    public int RegionServerPkey { get; set; }

    public int Data2 { get; set; }
}

//data in local, region server and main server database
class Type3 : IMainData
{
    public int Pkey { get; set; }
    public int RegionServerPkey { get; set; }
    public int MainServerPkey { get; set; }

    public DateTime Data3 { get; set; }
}

the reason that these classes don't inherit each other is, they are EF generated classes from database, and contains different fields in each one.

now I want to know whether a type is directly implemented from an interface, such as, Type1 is directly implement ILocalData, but Type2 and Type3 are not.

interfaceType.IsAssignableFrom(classType) not work, because it will check higher interface from that class.

classType.GetInterfaces().Except(classType.BaseType.GetInterfaces()).Contains(interfaceType) from another question not work because they are not inherit from each other.

Is this possible?





Aucun commentaire:

Enregistrer un commentaire