lundi 24 juillet 2017

Simpler way of accessing custom attributes?

I have a property of the "Form1" class called "InfoTest" that has some custom attributes that I want to access.

The code works fine, but is a bit unwieldy:

[Test("foo",15)]
    public double InfoTest { get; set; }

    public void RetrieveAttribute()
    {
        PropertyInfo field_info = typeof(Form1).GetProperty("InfoTest");
        object[] custom_attributes = field_info.GetCustomAttributes(typeof(TestAttribute), false);
        TestAttribute thisAttribute = (TestAttribute)custom_attributes[0];
        Debug.WriteLine(thisAttribute.Info + "," + thisAttribute.TheValue);
    }

I presume the answer is "No", but is there a simpler way of getting the attributes for InfoTest, that doesn't involve the `typeof(Form1).GetProperty("InfoTest")? I can't go (for example):

    var runtimePropInfo = InfoTest.GetType().GetRuntimeProperties();
    var propInfo = InfoTest.GetType().GetProperties();

...Which is essentially because it is trying to get the properties of a "double", not a "InfoTest" object.





Aucun commentaire:

Enregistrer un commentaire