views:

268

answers:

3

Hello. I'd like to know if my textBox1 variable has the ABCAttribute. How can I check this?

+6  A: 

You need a handle to the class (type) in which textBox1 exists:

Type myClassType = typeof(MyClass);

MemberInfo[] members = myClassType.GetMember("textBox1",
        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

if(members.Length > 0) //found a member called "textBox1"
{
    object[] attribs = members[0].GetCustomAttributes(typeof(ABCAttribute));

    if(attribs.Length > 0) //found an attribute of type ABCAttribute
    {
        ABCAttribute myAttrib = attribs[0] as ABCAttribute;
        //we know "textBox1" has an ABCAttribute,
        //and we have a handle to the attribute!
    }
}

This is a bit nasty, one possibility is to roll it into an extension method, used like so:

MyObject obj = new MyObject();
bool hasIt = obj.HasAttribute("textBox1", typeof(ABCAttribute));

public static bool HasAttribute(this object item, string memberName, Type attribute)
{
    MemberInfo[] members = item.GetType().GetMember(memberName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    if(members.Length > 0)
    {
        object[] attribs = members[0].GetCustomAttributes(attribute);
        if(attribs.length > 0)
        {
            return true;
        }
    }
    return false;
}
Rex M
Isn't there a way to do it without reflection? That doesn't seem elegant. I'd like to know if there's a way like textBox1.HasAttribute() or something.
devoured elysium
Nope. You have to use reflection to see attributes.
David
@JorgeBranco unfortunately no, attributes are very much tied to the reflection aspect of the language. However, you can wrap this all in an extension method.
Rex M
Point is, from what I understand, attributes would serve to ease up certains parts of coding. And from what I've seen, having to through all that code just to check if something has an attribute, doesn't ease up anything. What am I getting wrong?
devoured elysium
@Jorge please see my comment on @Joel's answer, and add more info to your question so we can give more specific answers to accomplish what you need.
Rex M
I don't get what all this fuss about attributes is. I am not totally convinced, although I will use your method in program to try this thing out. Thanks for the effort!
devoured elysium
Hello again. I've tried your code sample, but it doesn't seem to work, as it always says that members.Length = 0. What might be wrong? I had to rename members.length to .Length, and GetCustomAttributes(attribute) to (attribute, true).
devoured elysium
MemberInfo seems to be related to Methods, not variables.
devoured elysium
@Jorge MethodInfo refers to methods, MemberInfo can refer to any member. Make sure the BindingFlags options are not inadvertently excluding the member you want.
Rex M
+2  A: 

Assuming textBox1 is, well, a TextBox control, then the answer is likely, "No, it doesn't have the attribute." Attributes are assigned to a Type, not an instance of the type. You can lookup what attributes are on any TextBox that ever was, is, or will be created right now (for a particular version of the framework).

Joel Coehoorn
I think he means on the member.
Rex M
I have a couple of textboxes in my form, and I'd like to give to each one of them an attribute i did myself, which would set a MinValue and MaxValue(as the textboxes only accept numbers). Then, I'd have a method check all textboxes to see if they have the attributes(as not all the textboxes would have the my attribute set). If they had, and the value was within the allowed range, it'd then save the values.I thought this would be one of the few things were attributes could come handy, but i guess i was wrong. What is the use of custom attributes, then?
devoured elysium
@Jorge that is a very good use of custom attributes (although I might make the argument that Validation controls would work better in this specific case). The method I've described in my answer does exactly what you need, just going one step further of checking the Min and Max properties of the ABCAttribute once you have it.
Rex M
Yes, I undertood it does. Thing is that if we have to go through all that code(I know that it is not THAT many code, but it's ugly), wouldn't it just be easier to make a collection of structs OptionsTextBox:struct OptionsTextBoxStruct {TextBox _txtBox;int _min;int _max;}and then check each item of the collection to see if the given .Text of _txtBox would be between _min and _max?
devoured elysium
@Jorge I would argue no, since you can hide the functionality of determining attributes into a single method (see SRP) and never deal with it again; and the extra metadata is intrinsically part of your member declaration.
Rex M
A: 

Do you mean the attributes as in:

<input class="textbox" type="text" value="search" ABC="val" name="q"/>

In that case you can look up the attribute name in the control's Attribute collection.

WebForm Textbox control Attributes collection

If you mean attributes as in:

<ValidationPropertyAttribute("Text")> _
<ControlValuePropertyAttribute("Text")> _
Public Class TextBox _ 
...

then as the other posters have mentioned, you will have to use Reflection to determine if the control has a particular attribute.

Amal Sirisena