只有当C#中的属性继承自某个基类时,我才能递归搜索这些属性

本文关键字:递归 搜索 属性 基类 属性继承 | 更新日期: 2023-09-27 18:20:02

只有当属性的类型继承自某个基类时,我才能递归地获取对象的所有属性?

这是我的尝试:

static IEnumerable<PropertyInfo> FindProperties(object objectTree, Type targetType)
{
    if (objectTree.GetType().IsAssignableFrom(targetType))
    {
        var properties = objectTree.GetType().GetProperties();
        foreach (var property in properties)
        {
            yield return property;
        }
        foreach (var property in FindProperties(properties, targetType))
        {
            yield return property;
        }
    }
}

所以我可以打电话给

var allPropertiesOfPageTypes = FindProperties(someClass, typeof(Page));

但是,返回的属性数始终为零。我做错了什么?

编辑:

我不确定这是否重要,但子类是泛型类:

public abstract class MasterPage<T> : BasePage<T> where T : MasterPage<T>

继承:

public abstract class BasePage<T> : Page where T : BasePage<T>

从Master/BasePage继承的东西似乎对IsAssignableFrom返回false?

只有当C#中的属性继承自某个基类时,我才能递归搜索这些属性

只有当您拥有正确的类型,并且您需要一个实例,而不是属性本身时,才需要递归:

static IEnumerable<PropertyInfo> FindProperties(object objectTree, Type targetType)
{
    if (targetType.IsAssignableFrom(objectTree.GetType()))
    {
        var properties = objectTree.GetType().GetProperties();
        foreach (var property in properties)
        {
            yield return property;
            if (targetType.IsAssignableFrom(property.PropertyType))
            {
                object instance = property.GetValue(objectTree, null);
                foreach (var subproperty in FindProperties(instance, targetType))
                {
                    yield return subproperty;
                }
            }
        }
    }
}

为了验证对象是否从另一个类继承,您必须执行与您正在执行的操作相反的操作:

 if (targetType.IsAssignableFrom(objectTree.GetType()))

这与的工作方式类似

Parent o = new Derived();

也许这可行?

public static LinkedPageElement<TElement> GetLinkedElement<TPage, TElement>(Page page, bool verbose = true) where TElement : class
{
    var propInfos = page.GetType().GetProperties();
    // First try to find the property in the current page type
    foreach (var propInfo in propInfos)
    {
        var attributes = propInfo.GetCustomAttributes(typeof(LinkedPageAttribute), true);
        if (attributes.Length == 0) continue;
        var linkedPageAttribute = (from a in attributes where a.GetType() == typeof(LinkedPageAttribute) select a).FirstOrDefault();
        if (linkedPageAttribute == null || !(linkedPageAttribute is LinkedPageAttribute)) continue;
        if ((linkedPageAttribute as LinkedPageAttribute).PageType == typeof(TPage))
        {
            return new LinkedPageElement<TElement>
            {
                Element = propInfo.GetValue(page, null) as TElement,
                AutoClick = (linkedPageAttribute as LinkedPageAttribute).AutoClick
            };
        }
    }
    // Then try to find it in a property
    var containedInProperty = propInfos.Where(x => x.PropertyType.IsSubclassOf(typeof(Page)))
        .Select(x => GetLinkedElement<TPage, TElement>((Page)x.GetValue(page, null), false))
        .FirstOrDefault(x => x != null);
    if (containedInProperty != null) return containedInProperty;
    // you are trying to navigate to a page which cannot be reached from the current page, check to see you have a link with LinkedPage attribute
    if (verbose)
        throw new ArgumentException("You don't have a link to this page {0} from this page {1}".FormatWith(typeof(TPage), page.GetType()));
    return null;
}