Get the name of a property and class in a DisplayNameAttribute derived class

I have a class with some properties with the DisplayNameEx attribute that is derived from DisplayNameAttibute:

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

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

    }
}

I pass the string name ID ALLWAYS as a name, so it would be easier to write code this way:

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

The name of the property. I can get the CallerMemberName attribute here:

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

Can I also get the class name?

+4
source share
2 answers

It's impossible. The best way in your case is a parameter with the class name in the attribute constructor:

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; }
}
0
source

.NET 4 localizable DisplayAttribute. .NET 4 DisplayAttribute, .

- .

  • . , , . . .

  • . , , , .

, .

0

All Articles