Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#-使用Linq获取属性的属性_C#_Linq_Attributes - Fatal编程技术网

C#-使用Linq获取属性的属性

C#-使用Linq获取属性的属性,c#,linq,attributes,C#,Linq,Attributes,我有一个属性,它本身就有属性。我想访问其中一个属性(布尔值)并检查它是否为真。我可以检查属性是否已设置,但仅此而已。。至少对林克来说是这样 属性: public class ImportParameter : System.Attribute { private Boolean required; public ImportParameter(Boolean required) { this.required = required; } } 例

我有一个属性,它本身就有属性。我想访问其中一个属性(布尔值)并检查它是否为真。我可以检查属性是否已设置,但仅此而已。。至少对林克来说是这样

属性:

public class ImportParameter : System.Attribute
{
    private Boolean required;

    public ImportParameter(Boolean required)
    {
        this.required = required;
    }
}
例如:

    [ImportParameter(false)]
    public long AufgabeOID { get; set; }
到目前为止,我所拥有的:

        var properties = type.GetProperties()
            .Where(p => Attribute.IsDefined(p, typeof(ImportParameter)))
            .Select(p => p.Name).ToList();

我玩了一会儿,但似乎无法验证是否设置了所需的属性。

您必须公开所需的属性,如

public class ImportParameter : System.Attribute
{
  public Boolean Required {get; private set;}

  public ImportParameter(Boolean required)
  {
      this.Required = required;
  }
}
现在,您应该能够访问您的属性对象

请注意,通过使用
public{get;private set;}
您的属性可以作为public访问,但只能设置为private

下面是一个完整的工作示例:

using System;
using System.Linq;

public class Program
{
    [ImportParameter(false)]
    public Foo fc {get;set;}

    public static void Main()
    {       
        var required = typeof(Program).GetProperties()
            .SelectMany(p => p.GetCustomAttributes(true)
                          .OfType<ImportParameter>()
                          .Select(x => new { p.Name, x.Required }))
            .ToList();

        required.ForEach(x => Console.WriteLine("PropertyName: " + x.Name + " - Required: " + x.Required));
    }
}

public class ImportParameter : System.Attribute
{
  public Boolean Required {get; private set;}

  public ImportParameter(Boolean required)
  {
      this.Required = required;
  }
}

public class Foo 
{
    public String Test = "Test";
}
使用系统;
使用System.Linq;
公共课程
{
[输入参数(假)]
公共Foo fc{get;set;}
公共静态void Main()
{       
var required=typeof(程序).GetProperties()
.SelectMany(p=>p.GetCustomAttributes(true)
第()类
.Select(x=>new{p.Name,x.Required}))
.ToList();
required.ForEach(x=>Console.WriteLine(“PropertyName:+x.Name+”-required:+x.required));
}
}
公共类ImportParameter:System.Attribute
{
必需的公共布尔值{get;private set;}
公共导入参数(需要布尔值)
{
这个.必需的=必需的;
}
}
公开课Foo
{
公共字符串Test=“Test”;
}

首先,如果您想访问所需字段以使其公开,最好使用公共属性:

public class ImportParameter : System.Attribute
{
  public Boolean Required {get; private set;}

  public ImportParameter(Boolean required)
  {
      this.Required = required;
  }
}
然后,可以使用此查询Linq搜索所需属性设置为false的属性:

var properties = type.GetProperties()
            .Where(p => p.GetCustomAttributes() //get all attributes of this property
                         .OfType<ImportParameter>() // of type ImportParameter
                         .Any(a=>a.Required == false)) //that have the Required property set to false
            .Select(p => p.Name).ToList();
var properties=type.GetProperties()
.Where(p=>p.GetCustomAttributes()//获取此属性的所有属性
.OfType()//类型为ImportParameter
.Any(a=>a.Required==false))//将Required属性设置为false的
.Select(p=>p.Name).ToList();

必填项
似乎是一个字段,而不是属性。