Anttu
Anttu

Reputation: 1114

Is it possible to check what named parameters are set in an attribute

Is it possible in an C# attribute to explicitly check what named parameters are set?

The problem is that I have a couple of parameters of type bool in an attribute, and I want to explicitly check which one of them are set. I know I could probably make them nullable and check against that in the code, but is there a better way?

Upvotes: 0

Views: 223

Answers (4)

ControlPower
ControlPower

Reputation: 610

Please try:

[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
    private Nullable<int> _PropertyI;
    private Nullable<bool> _PropertyB;
    private String _Name;

    public int PropertyI
    {
        get
        {
            return _PropertyI.HasValue ? _PropertyI.Value : default(int);
        }
        set
        {
            this._PropertyI = value;
        }
    }

    public bool PropertyB
    {
        get
        {
            return _PropertyB.HasValue ? _PropertyB.Value : default(bool);
        }
        set
        {
            this._PropertyB = value;
        }
    }

    public String Name
    {
        get
        {
            return this._Name;
        }
        set
        {
            this._Name = value;
        }
    }

    public String[] GetAvailableParameters()
    {
        IList<String> names = new List<String>();

        Type type = this.GetType();
        FieldInfo[] fields
            = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic);
        foreach (FieldInfo field in fields)
        {
            Type fieldType = field.FieldType;
            Object fieldValue = field.GetValue(this);
            if (fieldValue != null)
            {
                names.Add(field.Name.Substring(1));
            }
        }

        return names.ToArray();
    }
}

Upvotes: 0

Rami Alshareef
Rami Alshareef

Reputation: 7140

Bool value either true or false, and the default value for bool variables is false, so you cant know whether the value of a bool var is set or not without define them as nullable then check nullity, or you can create a flag for each boolean property you have!

Why you need that anyway? isnt this check enough for you

if(myBoolProp)
{
   //My boolean var is checked, lets do something
}
else
{
   //My boolean var not checked :( cant do anything
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500225

This appears to do what you want, assuming you control the attribute itself:

using System;

[AttributeUsage(AttributeTargets.All)]
class SampleAttribute : Attribute
{
    private bool hasFlag = false;
    public bool HasFlag { get { return hasFlag; } }

    private bool flag = false;
    public bool Flag
    {
        get { return flag; }
        set
        {
            flag = value;
            hasFlag = true;
        }
    }
}

class Test
{
    static void Main()
    {
        foreach (var method in typeof(Test).GetMethods())
        {
            var attributes = (SampleAttribute[])
                method.GetCustomAttributes(typeof(SampleAttribute), false);
            if (attributes.Length > 0)
            {
                Console.WriteLine("{0}: Flag={1} HasFlag={2}",
                                  method.Name,
                                  attributes[0].Flag,
                                  attributes[0].HasFlag);
            }
        }
    }

    [Sample(Flag = true)]
    public static void WithFlagTrue() {}

    [Sample(Flag = false)]
    public static void WithFlagFalse() {}

    [Sample]
    public static void WithoutFlag() {}
}

Results:

WithFlagTrue: Flag=True HasFlag=True
WithFlagFalse: Flag=False HasFlag=True
WithoutFlag: Flag=False HasFlag=False

I'm not sure whether it's really a good idea, mind you...

Upvotes: 1

ControlPower
ControlPower

Reputation: 610

If this attribute is defined by you, you could provide a method to return all parameters which are set. If not, i think your question equals to how to check what properties are set in an object.

Upvotes: 0

Related Questions