설명 특성이있는 값이 각각있는 비트 열거 형을 사용했습니다.bitwise 열거 형의 특성을 얻는 방법
[Flags]
public enum ParkingAreaType
{
[Description("Car Park")]
CarPark = 1,
[Description("Residential Permit")]
ResidentialPermitZone = 2,
[Description("Commercial Permit")]
CommercialPermitZone = 4,
[Description("On Street Parking")]
OnStreetParking = 8,
Any =
CarPark | ResidentialPermitZone | CommercialPermitZone
}
내가 사용하고 난이
Getting attributes of Enum's value
매우 유사 몇 가지 다른 질문에 발견 여러 확장 방법을 시도했습니다 How to get attributes of enum
임은 현재 코드의 상기 SNIPPIT를 사용John Skeets EnumInternals에서 https://github.com/jskeet/unconstrained-melody/blob/master/UnconstrainedMelody/EnumInternals.cs
private static string GetDescription(T value)
{
FieldInfo field = typeof(T).GetField(value.ToString());
return field.GetCustomAttributes(typeof(DescriptionAttribute), false)
.Cast<DescriptionAttribute>()
.Select(x => x.Description)
.FirstOrDefault();
}
위의 코드는 단지 하나의 열거 형을 전달하면 작동하지만 비트 열거 형을 전달하면 Object Reference가 throw됩니다. 나는 여러 가지 다른 예제를 통해 위의 몇 가지 다른 변형을 시도했지만 모두 같은 결과를 얻었습니다. 에 전달되는 설명의 목록을 얻을 수있는 방법
난 그냥 문이 안 열려 알아낼. 더 필드가 예를 들어 CarPark | ResidentialPermitZone | CommercialPermitZone
(또는 CarPark | ResidentialPermitZone
등)가 호출되지 때문에
당신은 할 것입니다
감사합니다. – InitLipton