开发者

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 probl开发者_Go百科em 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?


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...


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.


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
}


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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜