4

I have a class with some properties with DisplayNameEx attribute, which is derived from DisplayNameAttibute:

public class Settings
{
    [DisplayNameEx("User")]
    public UserData User { get; set; }
}

public class DisplayNameExAttribute : DisplayNameAttribute
{
    public DisplayNameExAttribute(string id)
    {

    }
}

I pass as string id ALLWAYS name of my property, so it would be easier writing code this way:

public class Settings
{
    [DisplayNameEx()]
    public UserData User { get; set; }
}

Property name I can get here with CallerMemberName attribute:

public class DisplayNameExAttribute : DisplayNameAttribute
{
    public DisplayNameExAttribute([CallerMemberName] string propertyName = null)
    {
        //propertyName I get, what about class name (Settings in my case)?
    }
}

Is it possible to get class name also?

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
Pyko
  • 79
  • 5
  • possible duplicate of [How to get name of property which our attribute is set?](http://stackoverflow.com/questions/4606973/how-to-get-name-of-property-which-our-attribute-is-set) – Joe Farrell Apr 18 '15 at 21:10
  • possible duplicate of [How do I get the member to which my custom attribute was applied?](http://stackoverflow.com/questions/2168942/how-do-i-get-the-member-to-which-my-custom-attribute-was-applied) – Chris Apr 18 '15 at 21:10
  • @JoeFarrell: thanks for that one. I've just edited my question, is there a solution for gettng class name also? – Pyko Apr 18 '15 at 21:37
  • 1
    While I unserstand your goal, this has quite a bit of a design smell IMHO. The attributes shouldn't ever be aware of which members they are being applied (constructed) to. You should probably put this logic there where you consume your custom attribute, like for example falling back to the propertyname if the string is null or empty in the attribute. – Zoltán Tamási Apr 18 '15 at 21:55
  • @ZoltánTamási: i need this for Localization purpose. In localzation file I'll use resourceID as className.propertyName. I simply want to decorate my properties just [DisplayNameEx] attribute, not with [DisplayNameEx("className.propertyName")]. – Pyko Apr 19 '15 at 07:07

2 Answers2

0

It's impossible. Best way in your case is parameter with class name in the constructor of the attribute:

public class DisplayNameExAttribute : DisplayNameAttribute
{
    public DisplayNameExAttribute(string className = null)
    {

    }
}

and then use it like this:

public class Settings
{
    [DisplayNameEx("Settings")]
    public UserData User { get; set; }
}
Alexander Imra
  • 832
  • 5
  • 11
0

I used a similar technique before .NET 4's localizable DisplayAttribute. From .NET 4 I use the DisplayAttribute because it supports localization and it's much more powerful.

I personally would not recommend your idea because of mainly two reasons.

  1. Potential refactoring issues. If one renames your property, he doesn't have any idea that he should also rename the resource with the same name. Same stands for class names. After a few months even you can forget it time to time.

  2. The code is not straightforward. Other developers could not easily understand what your attributes are doing, and even hard to see at first glance that it's related to localization.

I think the best is just to put there those strings.

Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93