예를 들어 가장 잘 볼 수 있습니다. 내가 인스턴스에서 해당 속성에 도달 할누구나 enum 값에 대한 사용자 지정 특성을 빠르게 얻을 수있는 방법을 알고 있습니까?
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,
}
: 나는 속성을 열거가 좀 느림을 기대 반사를 사용하고이 같이
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
}
하지만 열거를 변환 더러워 보인다 값을 이미 문자열이있는 문자열 (이름을 반영)로 변환합니다.
누구에게 더 좋은 방법이 있습니까?
'Enum.GetName()'과 비교해 보셨습니까? –