Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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#_.net_Linq_Enums - Fatal编程技术网

C# 按属性枚举值筛选PropertyInfo

C# 按属性枚举值筛选PropertyInfo,c#,.net,linq,enums,C#,.net,Linq,Enums,我的每个模型都有一些自定义属性,如下所示 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class FieldAttribute : System.Attribute { private FieldFor _fieldFor; public FieldAttribute(FieldFor fieldFor) { _fieldFor = fieldFor;

我的每个模型都有一些自定义属性,如下所示

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class FieldAttribute : System.Attribute
{
    private FieldFor _fieldFor;

    public FieldAttribute(FieldFor fieldFor)
    {
        _fieldFor = fieldFor;
    }
}
FieldFor类型是枚举。所以我可以对这样一个域声明这个

[Field(FieldFor.Name)]
public string Name { get; set; }
现在,为了获得模型上具有此自定义属性的属性列表,我使用以下命令

List<PropertyInfo> props = new MyModel().GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(FieldAttribute))).ToList<PropertyInfo>();
List props=new MyModel().GetType().GetProperties().Where(prop=>Attribute.IsDefined(prop,typeof(FieldAttribute))).ToList();
现在我有了所有具有自定义属性的属性的列表,我如何才能得到哪个属性的FieldFor值为Name


我分别执行这两个查询,因为我必须获取模型上许多属性的值

您可以使用方法获取属性,然后可以访问其成员:

foreach(var prop in props)
{
    var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
    var value = fieldAttribute.FieldFor;
}
使用linq:

props.Where(x => x.GetCustomAttribute<FieldAttribute>().FieldFor == FieldFor.Name);
props.Where(x=>x.GetCustomAttribute().FieldFor==FieldFor.Name);

您可以使用方法获取属性,然后可以访问其成员:

foreach(var prop in props)
{
    var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
    var value = fieldAttribute.FieldFor;
}
使用linq:

props.Where(x => x.GetCustomAttribute<FieldAttribute>().FieldFor == FieldFor.Name);
props.Where(x=>x.GetCustomAttribute().FieldFor==FieldFor.Name);

您可以使用方法获取属性,然后可以访问其成员:

foreach(var prop in props)
{
    var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
    var value = fieldAttribute.FieldFor;
}
使用linq:

props.Where(x => x.GetCustomAttribute<FieldAttribute>().FieldFor == FieldFor.Name);
props.Where(x=>x.GetCustomAttribute().FieldFor==FieldFor.Name);

您可以使用方法获取属性,然后可以访问其成员:

foreach(var prop in props)
{
    var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
    var value = fieldAttribute.FieldFor;
}
使用linq:

props.Where(x => x.GetCustomAttribute<FieldAttribute>().FieldFor == FieldFor.Name);
props.Where(x=>x.GetCustomAttribute().FieldFor==FieldFor.Name);
您可以执行以下操作:

var props = new MyModel()
    .GetType()
    .GetProperties()
    .Where(prop =>
        {
            var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
            if (fieldAttribute != null)
            {
                return fieldAttribute.FieldFor == FieldFor.Name;
            }

            return false;
        })
    .ToList();
var props=new MyModel()
.GetType()
.GetProperties()
.其中(prop=>
{
var fieldAttribute=prop.GetCustomAttribute();
if(fieldAttribute!=null)
{
返回fieldAttribute.FieldFor==FieldFor.Name;
}
返回false;
})
.ToList();
您可以执行以下操作:

var props = new MyModel()
    .GetType()
    .GetProperties()
    .Where(prop =>
        {
            var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
            if (fieldAttribute != null)
            {
                return fieldAttribute.FieldFor == FieldFor.Name;
            }

            return false;
        })
    .ToList();
var props=new MyModel()
.GetType()
.GetProperties()
.其中(prop=>
{
var fieldAttribute=prop.GetCustomAttribute();
if(fieldAttribute!=null)
{
返回fieldAttribute.FieldFor==FieldFor.Name;
}
返回false;
})
.ToList();
您可以执行以下操作:

var props = new MyModel()
    .GetType()
    .GetProperties()
    .Where(prop =>
        {
            var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
            if (fieldAttribute != null)
            {
                return fieldAttribute.FieldFor == FieldFor.Name;
            }

            return false;
        })
    .ToList();
var props=new MyModel()
.GetType()
.GetProperties()
.其中(prop=>
{
var fieldAttribute=prop.GetCustomAttribute();
if(fieldAttribute!=null)
{
返回fieldAttribute.FieldFor==FieldFor.Name;
}
返回false;
})
.ToList();
您可以执行以下操作:

var props = new MyModel()
    .GetType()
    .GetProperties()
    .Where(prop =>
        {
            var fieldAttribute = prop.GetCustomAttribute<FieldAttribute>();
            if (fieldAttribute != null)
            {
                return fieldAttribute.FieldFor == FieldFor.Name;
            }

            return false;
        })
    .ToList();
var props=new MyModel()
.GetType()
.GetProperties()
.其中(prop=>
{
var fieldAttribute=prop.GetCustomAttribute();
if(fieldAttribute!=null)
{
返回fieldAttribute.FieldFor==FieldFor.Name;
}
返回false;
})
.ToList();

我可以用linq查询来代替foreach吗?我刚刚意识到我自己没有公共财产,对吧!谢谢你,完美的回答,我能用linq查询来代替foreach吗?我刚刚意识到我自己没有公共财产,duh!谢谢你,完美的回答,我能用linq查询来代替foreach吗?我刚刚意识到我自己没有公共财产,duh!谢谢你,完美的回答,我能用linq查询来代替foreach吗?我刚刚意识到我自己没有公共财产,duh!谢谢你,完美的答案