Bu, muhtemelen en iyi örneklerle gösterilmektedir. Ben örneğinden bu özelliklerle almak istiyorumHerkes enum değerinde özel özelliklere ulaşmanın hızlı bir yolunu biliyor mu?
public enum MyEnum {
[CustomInfo("This is a custom attrib")]
None = 0,
[CustomInfo("This is another attrib")]
ValueA,
[CustomInfo("This has an extra flag", AllowSomething = true)]
ValueB,
}
: Ben özelliklere sahip bir enum var bazı yavaşlık bekliyoruz yansıma kullanıyor bu gibi
public CustomInfoAttribute GetInfo(MyEnum enumInput) {
Type typeOfEnum = enumInput.GetType(); //this will be typeof(MyEnum)
//here is the problem, GetField takes a string
// the .ToString() on enums is very slow
FieldInfo fi = typeOfEnum.GetField(enumInput.ToString());
//get the attribute from the field
return fi.GetCustomAttributes(typeof(CustomInfoAttribute ), false).
FirstOrDefault() //Linq method to get first or null
as CustomInfoAttribute; //use as operator to convert
}
, ancak enum dönüştürmek için dağınık görünüyor Zaten bir örneğim olduğunda bir dizeye (adı yansıtan) değer.
Daha iyi bir yolu olan var mı?
Enum.GetName() 'ile karşılaştırdınız mı? –