1

I have a class like this

public class AppFlags
{
    [Description("title")]
    public static bool ShouldRemoveTitle = false;
}

I want to get the description attribute value "title" of field ShouldRemoveTitle. How to do that as a fast way.

I saw a solution here but it must copy field name "ShouldRemoveTitle" as parameter, which make function is too hard to use Extract Description Attribute from Const Fields

HelloWindowsPhone
  • 680
  • 10
  • 19
  • 1
    Then just create a wrapper function that passes the name, so you don't have to pass it as a parameter? It's not clear why the linked question wouldn't answer your question. – Jeroen Mostert Dec 16 '19 at 16:05
  • 3
    Does this answer your question? [Extract Description Attribute from Const Fields](https://stackoverflow.com/questions/33485932/extract-description-attribute-from-const-fields) – Joel Trauger Dec 16 '19 at 16:12

1 Answers1

1

It is the same as on any other field. Just with the BindingFlag set to static

var attribute = typeof(AppFlags)
   .GetField("ShouldRemoveTitle", BindingFlags.Public | BindingFlags.Static)
   ?.GetCustomAttribute<DiscriptionAttribute>(true);
string text = attribute?.Description;
Holger
  • 2,446
  • 1
  • 14
  • 13
  • I would like any method that just call AppFlags.ShouldRemoveTitle.GetDescription() – HelloWindowsPhone Dec 17 '19 at 00:38
  • you should be able to build a (static)method around this one line of code. You can give it the name AppFlags_ShouldRemoveTitle_GetDescription() if you like. – Holger Dec 17 '19 at 22:34