brocrow
brocrow

Reputation: 65

C# - Get property of Attribute with Linq

I have a Attribute which has properties itself. I would like to access one of These properties (a boolean) and check whether it's true or not. I was able to check whether the Attribute is set, but thats about all .. at least with linq.

Attribute:

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

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

Example:

    [ImportParameter(false)]
    public long AufgabeOID { get; set; }

What I have so far:

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

I played around a Little, but I don't seem to be able to verify whether the property required is set or not.

Upvotes: 3

Views: 4453

Answers (2)

Markus Weninger
Markus Weninger

Reputation: 12648

You have to make the required property public, like

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

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

Now you should be able to access your attributes object.

Notice that by using public <DataType> <Name> {get; private set;} your property is accessible as public, but can only be set private.

Following a complete working example:

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

Upvotes: 2

Alberto
Alberto

Reputation: 15941

First of all if you want to access the required field you need to make it public, better a public property:

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

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

then you can use this query Linq to search for properties that have the Required attribute set to 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();

Upvotes: 4

Related Questions