0

I'm trying to find a way to get access to attributes which have been applied at the property level from within an object, but am hitting a brick wall. It appears that the data is stored at the type level, but I need it at the property level.

Here's an example of what I'm trying to make:

public class MyClass
{
    [MyAttribute("This is the first")]
    MyField First;
    [MyAttribute("This is the second")]
    MyField Second;
}

public class MyField
{
    public string GetAttributeValue()
    {
        // Note that I do *not* know what property I'm in, but this
        // should return 'this is the first' or 'this is the second',
        // Depending on the instance of Myfield that we're in.
        return this.MyCustomAttribute.value;  
    }
}
  • [Is this](http://stackoverflow.com/questions/2329738/how-to-get-a-custom-attribute-from-object-instance-in-c-sharp) what you're looking for? – Michael Jun 09 '15 at 20:38
  • Try here: http://stackoverflow.com/questions/23329087/get-custom-attribute-from-specific-object-property-field – dbc Jun 09 '15 at 20:38
  • Also here: http://stackoverflow.com/questions/8948522/c-sharp-custom-attributes-from-properties – dbc Jun 09 '15 at 20:39

1 Answers1

0

Attributes aren't used that way. Attributes are stored in the class object that contains the attribute, not the member object/field that is declared within the class.

Since you want this to be unique per member, it feels more like this should be member data with MyField, something passed in by the constructor.

If you're hell bent on using an attribute, you could add code in your constructor that used reflection on every member that has an attribute attached to it an attempts to set its instance data to what you want, but you would need to make sure that all your MyField objects are fully constructed.

Alternately you could make your setter look like this:

private MyField _first;
[MyAttribute("This is the first")]
public MyField First {
    get { return _first; }
    set {
        _first = value;
        if (_first != null) {
            _first.SomeAttribute = GetMyAttributeValue("First");
        }
    }
 }

 private string GetMyAttributeValue(string propName)
 {
     PropertyInfo pi = this.GetType().GetPropertyInfo(propName);
     if (pi == null) return null;
     Object[] attrs = pi.GetCustomAttributes(typeof(MyAttribute));
     MyAttribute attr =  attrs.Length > 0 ? attrs[0] as MyAttribute : null;
     return attr != null ? attr.Value : null;
 }
plinth
  • 48,267
  • 11
  • 78
  • 120
  • Not what I wanted to hear, but unfortunately this appears to be the end of the story. It seems weird to me that you can add custom attributes to individual properties of a specific class, but you cannot use them inside the instance of the class to modify behavior. I understand that this scenario is well-suited for a property on the class, I was just trying to get around an nhibernate bug which will not allow me to use a property for this. Thanks for your quick answer, Plinth! – Mike Taute Jun 10 '15 at 21:23
  • It makes sense if you think about attributes as metadata that accompanies the class. You can put attributes on a class, field, property or method and then that data is accessible from the `Type` object that defines those elements. – plinth Jun 11 '15 at 13:01
  • I see what you're saying, but that's precisely why it doesn't make sense to me. If attributes are metadata at the class level, then why do they take effect (or not) at the instance level? For example: [Required(true)] public int IsRequired; public int NotRequired; Only one *instance* of integer will have this attribute, not both. So if the metadata is at the class level.... I may be just misunderstanding everything, but it seems to me that the attribute is applied at the instance level or both integers would be affected. – Mike Taute Jun 20 '15 at 01:44