Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PropertyInfo来自子类属性_C#_Reflection_Custom Attributes_Propertyinfo - Fatal编程技术网

C# PropertyInfo来自子类属性

C# PropertyInfo来自子类属性,c#,reflection,custom-attributes,propertyinfo,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 LookU

我有两门课:

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
]属性,我试图获取属性,在数据库中进行搜索之后,可以对这些属性进行搜索

目前我有静态medthod:

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();
}
我如何才能获得具有“实名”属性的属性信息列表:

 - Model   
 - ManufacterName
提前谢谢。

试试看

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

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

像这样,

    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);
        }
    }
public static void GetAllSearchProperties WithOrderBy(类型,引用列表结果)
{
结果=结果??新列表();
foreach(GetSearchPropertiesWithOrderBy中的var propertyInfo(类型))
{
结果.添加(propertyInfo);
GetAllSearchPropertiesWithOrderBy(propertyInfo.PropertyType,ref result);
}
}

根据您的说明注释,您希望为嵌套实例提供其属性的名称。如果不想用实例的属性名命名包含实例的属性,请尝试以下操作

给定以下SearchableAttribute:

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

    public string SearchablePropertyName { get; set; }
}
您应该递归地提取属性的属性(这是一种深度优先的方法,也可以广度优先):

您不仅返回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();
}
公共静态列表GetSearchProperties WithOrderBy(类型)
{
if(type==null)
抛出新的ArgumentNullException(“类型”);
返回类型
.GetProperties()
.选择(
x=>
{
var searchableAttribute=GetNestedSearchableAttribute(x);
变量说明=新
{
属性=x,
属性=可搜索属性,
Name=searchableAttribute==null?x.名称:searchableAttribute.SearchablePropertyName
};
返回说明;
})
.OrderBy(x=>x.Attribute!=null?x.Attribute.OrderBy:200)
.Select(x=>Tuple.Create(x.Property,x.Name))
.ToList();
}

希望这就是您所需要的。

如果层次结构也是一、二、三、四级等,这将适用于所有类。唯一缺少的是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; }

    }

“真实名称”是什么意思?子类“LookUpdatea”的属性名称应该是“ManufactureName”,工作起来很有魅力。谢谢你的帮助,所有的单元测试都通过了。有些失败了,但是你的代码还可以,我的单元测试做错了。谢谢
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();
}
    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; }

    }
    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; }

    }