【问题标题】:Recursively search Properties in class decorated by attribute递归搜索由属性修饰的类中的属性
【发布时间】:2019-05-15 15:05:54
【问题描述】:

我想获取由属性 AggregateAuthorizeIdentifier 修饰的所有属性。它是核心功能,所以应该尽可能快。

我可以通过所有属性进行递归搜索,但我会检查所有系统和第 3 方库。但这不是最佳解决方案。

你遇到过这样的问题吗?

public class ReadOrderHistoryQuery
{
    public List<string> Ordering { get; set; }

    public Criteria Criteria { get; set; }

    public class Criteria
    {
        [AggregateAuthorizeIdentifier]
        public int UserId { get; set; }

        public string InvoiceId { get; set; }
    }
}

我的解决方案:

    static IEnumerable<PropertyWithAttribute> GetAuthorizeIdentifierAttribute(object command)
    {
        var attributedProperties = GetAuthorizeIdentifierAttribute(command.GetType());
        return attributedProperties;
    }
    static IEnumerable<PropertyWithAttribute> GetAuthorizeIdentifierAttribute(Type type)
    {
        //check current property
        var result = type.GetProperties()
            .Where(prop => Attribute.IsDefined(prop, typeof(AggregateAuthorizeIdentifierAttribute)))
            .Select(p => new PropertyWithAttribute()
            {
                Property = p,
                Attribute = p.GetCustomAttribute<AggregateAuthorizeIdentifierAttribute>(true)
            })
            .ToList();
        //deeper check all properties
        result.AddRange(type.GetProperties()
            .SelectMany(p => GetAuthorizeIdentifierAttribute(p.PropertyType)));
        return result;
    }

【问题讨论】:

  • But it isn't optimal solution. 你为什么这么认为?
  • 我的解决方案搜索通过所有系统属性,如字符串、列表等和第 3 方。这是完全没有必要的。

标签: c# .net recursion reflection attributes


【解决方案1】:

最后我通过额外的类型选择器(按字符串)实现了它。

    const string PropertyTypeStartWithSelector = "Diabdis.";

    static IEnumerable<PropertyWithAttribute> GetAuthorizeIdentifierAttribute(Type type, string propertyPathPrefix)
    {
        var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        //check current property
        var result = properties
            .Where(prop => Attribute.IsDefined(prop, typeof(AggregateAuthorizeIdentifierAttribute)))
            .Select(p => new PropertyWithAttribute()
            {
                PropertyPath = propertyPathPrefix == null ? p.Name : $"{propertyPathPrefix}{PropertyPathSeparator}{p.Name}",
                Attribute = p.GetCustomAttribute<AggregateAuthorizeIdentifierAttribute>(true)
            })
            .ToList();

        //check deeper properties, BUT just selector classes
        result.AddRange(properties
            .Where(p => p.DeclaringType.FullName.StartsWith(PropertyTypeStartWithSelector))
            .SelectMany(p => GetAuthorizeIdentifierAttribute(p.PropertyType, propertyPathPrefix == null ? p.Name : $"{propertyPathPrefix}{PropertyPathSeparator}{p.Name}")));
        return result;
    }

【讨论】:

    【解决方案2】:

    当我需要做类似的事情时,我总是这样做:

    object objectToSearch = ...
    
    PropertyInfo[] properties = objectToSearch.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
    if (properties != null && properties.Length > 0)
    {
        properties.ToList().ForEach(p =>
        {
            if (p.GetCustomAttributes(typeof(AggregateAuthorizeIdentifierAttribute), false).Count() == 1)
            {
                // Do something with the property
            }
        });
    }
    

    据此,您可以创建一个扩展方法来返回具有特定属性的对象的所有属性

    【讨论】:

    • 绑定很有用,但不能解决在系统模块内搜索的问题(如列表、字典等)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2010-09-06
    • 2011-01-23
    • 2020-03-15
    相关资源
    最近更新 更多