PropertyInfo来自SubClass属性

时间:2013-10-21 06:52:45

标签: c# reflection custom-attributes propertyinfo

我有两个班级:

public class LookUpData 
{
  [Searchable]
  public string ManufactureName {get;set;)
}

public class Car
{
   public int ID {get;set;}
   [Searchable]  
   public string  Model {get;set;}
   public int ManufactureId {get;set;} 
   public LookUpData LookUpData{ get;set;} 

}

使用[Searchable] attiribute我正试图在我可以在DB中搜索后获取属性。

目前我有静态方法:

public static List<PropertyInfo> GetSearchPropertiesWithOrderBy(Type type)
{
  if (type == null) throw new ArgumentNullException("type");

  return type.GetProperties()
             .Select(x => new
                            {
                              Property = x,
                              Attribute =
                            (SearchableAttribute) Attribute.GetCustomAttribute(x, typeof (SearchableAttribute), true)
                            })
             .OrderBy(x => x.Attribute != null ? x.Attribute.OrderBy : 200)
             .Select(x => x.Property)
             .ToList();
}

我可以获得PropertyInfo的列表

 - Model 
 - LookUpData

如何获取PropertyInfo列表以获得“真实姓名”属性:

 - Model   
 - ManufacterName

提前致谢。

4 个答案:

答案 0 :(得分:1)

根据您的澄清评论,您希望为嵌套实例指定其属性的名称。如果您不想为包含具有其属性名称的实例的属性命名,请尝试以下操作。

鉴于以下SearchableAttribute:

public class SearchableAttribute : Attribute
{
    public int OrderBy { get; set; }

    public string SearchablePropertyName { get; set; }
}

你应该递归地提取属性的属性(这是深度优先的方法,你也可以先做广度):

private static SearchableAttribute GetNestedSearchableAttribute(PropertyInfo propertyInfo)
{
    var attribute = (SearchableAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(SearchableAttribute), true);
    if (attribute == null)
    {
        return propertyInfo
            .PropertyType
            .GetProperties()
            .Select(GetNestedSearchableAttribute)
            .FirstOrDefault(attrib => attrib != null);
    }

    attribute.SearchablePropertyName = propertyInfo.Name;

    return attribute;
}

你不仅会返回PropertyInfo,还会返回一个信息以及描述,如下所示:

public static List<Tuple<PropertyInfo, string>> GetSearchPropertiesWithOrderBy(Type type)
{
    if (type == null)
        throw new ArgumentNullException("type");

    return type
        .GetProperties()
        .Select(
            x =>
            {
                var searchableAttribute = GetNestedSearchableAttribute(x);
                var description = new
                {
                    Property = x,
                    Attribute = searchableAttribute,
                    Name = searchableAttribute == null ? x.Name : searchableAttribute.SearchablePropertyName
                };

                return description;

            })
        .OrderBy(x => x.Attribute != null ? x.Attribute.OrderBy : 200)
        .Select(x => Tuple.Create(x.Property, x.Name))
        .ToList();
}

希望这就是你所需要的。

答案 1 :(得分:1)

这适用于所有类,如果层次结构也是一,二,三,四级等。只有缺少的是OrderBy。我相信一旦上市,你就可以完成这件事。

    public static List<PropertyInfo> GetSearchProperties(Type type)
    {
        if (type == null) throw new ArgumentNullException("type");


        List<PropertyInfo> propL = new List<PropertyInfo>();
        foreach (var item in type.GetProperties())
        {
            object[] obj = item.GetCustomAttributes(typeof(Searchable), true);
            if (obj.Count() > 0)
            {
                propL.Add(item);
            }
            else
            {                    
                propL.AddRange(GetSearchPropertiesWithOrderBy(item.PropertyType));
            }
        }
        return propL;        
    }
    [System.AttributeUsage(System.AttributeTargets.Property)]
    public class Searchable : System.Attribute
    {

    }
    public class LookUpData
    {
        [Searchable]
        public string ManufactureName { get; set; }
    }

    public class Car
    {
        public int ID { get; set; }
        [Searchable]
        public string Model { get; set; }
        public int ManufactureId { get; set; }
        public LookUpData LookUpData { get; set; }

    }

对于层次结构,它意味着您的LookUpData属性是否未直接在Car类中声明。例如,此代码也可以正常工作。

    public class MyDataModels
    {
        LookUpData MysetOfData { get; set; }
        int MyVal { get; set; }
    }

    public  class MyCar
    {
        public int ID { get; set; }
        [Searchable]
        public string Model { get; set; }
        public int ManufactureId { get; set; }
        public MyDataModels MyModel { get; set; }

    }

答案 2 :(得分:0)

尝试

.Select(x => new
             {
                 Property = x.Name,
                 ...

答案 3 :(得分:0)

您也可以使用其他递归方法来收集内部属性。

像这样,

    public static void GetAllSearchPropertiesWithOrderBy(Type type, ref List<PropertyInfo> result)
    {
        result = result ?? new List<PropertyInfo>();
        foreach (var propertyInfo in GetSearchPropertiesWithOrderBy(type))
        {
            result.Add(propertyInfo);
            GetAllSearchPropertiesWithOrderBy(propertyInfo.PropertyType, ref result);
        }
    }