简体   繁体   English

如何使用反射在泛型类型中动态确定属性属于基类还是子类?

[英]How to determine if the property belongs to Base class or sub class dynamically in generic type using reflection?

I have following two classes (models), one is base class and other is sub class:我有以下两个类(模型),一个是基类,另一个是子类:

public class BaseClass
{    
     public string BaseProperty{get;set;}    
}

public class ChildClass: BaseClass    
{    
     public string ChildProperty{get;set;}    
}

In application I am calling ChildClass dynamically using generics在应用程序中,我使用泛型动态调用ChildClass

List<string> propertyNames=new List<string>();
foreach (PropertyInfo info in typeof(T).GetProperties())
{
      propertyNames.Add(info.Name);
}

Here, in propertyNames list, I am getting property for BaseClass as well.在这里,在propertyNames列表中,我也获得了BaseClass属性。 I want only those properties which are in child class.我只想要那些在子类中的属性。 Is this possible?这可能吗?

What I tried?我尝试了什么?

  1. Tried excluding it as mentioned in this question尝试将其排除在此问题中
  2. Tried determining whether the class is sub class or base class as mentioned here but that does not help either.尝试确定该类是此处提到的子类还是基类,但这也无济于事。

You can try this你可以试试这个

foreach (PropertyInfo info in typeof(T).GetProperties()
        .Where(x=>x.DeclaringType == typeof(T))) // filtering by declaring type
{
    propertyNames.Add(info.Name);
}

...I want only those properties which are in child class. ...我只想要那些在子类中的属性。 Is this possible?这可能吗?

You need to use the GetProperties overload that takes a BindingFlags argument and include the BindingFlags.DeclaredOnly flag.您需要使用接受BindingFlags参数并包含BindingFlags.DeclaredOnly标志的GetProperties重载。

PropertyInfo[] infos = typeof(ChildClass).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

DeclaredOnly: Specifies that only members declared at the level of the supplied type's hierarchy should be considered. DeclaredOnly:指定仅应考虑在提供的类型层次结构级别声明的成员。 Inherited members are not considered.不考虑继承成员。

Using a simple loop to get the base class property names使用简单循环获取基类属性名称

var type = typeof(T);

var nameOfBaseType = "Object";

while (type.BaseType.Name != nameOfBaseType)
{
    type = type.BaseType;
}

propertyNames.AddRange(type.GetProperties().Select(x => x.Name))

I needed to get from the base class the derived class name and properties but nothing about the base class...我需要从基类获取派生类名称和属性,但没有关于基类的信息......

Based on the top answer I used the following... Hopefully it helps someone else!根据我使用的最佳答案...希望它可以帮助其他人!

public abstract class StorageObject
{
    protected readonly string TableName;
    protected readonly string[] ColumnNames;

    protected StorageObject()
    {
        TableName = GetType().Name;

        ColumnNames = GetType().GetProperties().Where(x => x.DeclaringType == GetType())
            .Select(x => x.Name)
            .ToArray();
    }
}

public class Symbol : StorageObject
{
    public string Name { get; private set; }
    public bool MarginEnabled { get; private set; }
    public bool SpotEnabled { get; private set; }
    public Symbol(ICommonSymbol symbol)
    {
        Name = symbol.CommonName;
        
        if (symbol is BitfinexSymbolDetails bsd)
        {
            MarginEnabled = bsd.Margin;
        }

        if (symbol is BinanceSymbol bs)
        {
            SpotEnabled = bs.IsSpotTradingAllowed;
            MarginEnabled = bs.IsMarginTradingAllowed;
        }
        
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM