Skip to content

Instantly share code, notes, and snippets.

@mzabsky
Created May 14, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzabsky/fd419593ff59a03dc1e1d53d38ff4c74 to your computer and use it in GitHub Desktop.
Save mzabsky/fd419593ff59a03dc1e1d53d38ff4c74 to your computer and use it in GitHub Desktop.
public class MyAttribute : Attribute {
public string Name {get;}
public MyAttribute(string name)
{
this.Name = name;
}
}
public enum TestEnum {
[My("Aa")] A = 3,
[My("Be")] B = 2,
[My("Ce")] C = 1
}
void Main()
{
var type = typeof(TestEnum);
var values = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
var info = values.Select(field =>
{
return new { field.Name, Value = (int)field.GetValue(null), Localization = field.GetCustomAttribute<MyAttribute>().Name };
});
}
/*
Vraci:
A 3 Aa
B 2 Be
C 1 Ce
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment