Bạn có thể phân tích các giá trị thuộc tính xml trở lại một kiểu enum với:
var value = Enum.Parse(typeof(Fuel), "B");
Nhưng tôi không nghĩ rằng bạn sẽ nhận được thực sự xa với các giá trị "đặc biệt" của bạn (3
, a/
vv). Tại sao bạn không xác định enum của mình là
enum Fuel
{
Benzine,
Diesel,
// ...
Three,
ASlash,
// ...
}
Và viết phương pháp tĩnh để chuyển đổi chuỗi thành thành viên enum? Một điều bạn có thể xem xét để thực hiện phương pháp như vậy là thêm thuộc tính tùy chỉnh vào các thành viên enum chứa biểu diễn chuỗi của chúng - nếu một giá trị không có đối tác chính xác trong điều tra, hãy tìm một thành viên với thuộc tính.
Tạo một thuộc tính như vậy là dễ dàng:
/// <summary>
/// Simple attribute class for storing String Values
/// </summary>
public class StringValueAttribute : Attribute
{
public string Value { get; private set; }
public StringValueAttribute(string value)
{
Value = value;
}
}
Và sau đó bạn có thể sử dụng chúng trong enum của bạn:
enum Fuel
{
[StringValue("B")]
Benzine,
[StringValue("D")]
Diesel,
// ...
[StringValue("3")]
Three,
[StringValue("/")]
Slash,
// ...
}
Hai phương pháp này sẽ giúp bạn phân tích một chuỗi thành một thành viên enum của bạn lựa chọn:
/// <summary>
/// Parses the supplied enum and string value to find an associated enum value (case sensitive).
/// </summary>
public static object Parse(Type type, string stringValue)
{
return Parse(type, stringValue, false);
}
/// <summary>
/// Parses the supplied enum and string value to find an associated enum value.
/// </summary>
public static object Parse(Type type, string stringValue, bool ignoreCase)
{
object output = null;
string enumStringValue = null;
if (!type.IsEnum)
{
throw new ArgumentException(String.Format("Supplied type must be an Enum. Type was {0}", type));
}
//Look for our string value associated with fields in this enum
foreach (FieldInfo fi in type.GetFields())
{
//Check for our custom attribute
var attrs = fi.GetCustomAttributes(typeof (StringValueAttribute), false) as StringValueAttribute[];
if (attrs != null && attrs.Length > 0)
{
enumStringValue = attrs[0].Value;
}
//Check for equality then select actual enum value.
if (string.Compare(enumStringValue, stringValue, ignoreCase) == 0)
{
output = Enum.Parse(type, fi.Name);
break;
}
}
return output;
}
Và trong khi tôi ở đó: đây là cách khác vòng;)
/// <summary>
/// Gets a string value for a particular enum value.
/// </summary>
public static string GetStringValue(Enum value)
{
string output = null;
Type type = value.GetType();
if (StringValues.ContainsKey(value))
{
output = ((StringValueAttribute) StringValues[value]).Value;
}
else
{
//Look for our 'StringValueAttribute' in the field's custom attributes
FieldInfo fi = type.GetField(value.ToString());
var attributes = fi.GetCustomAttributes(typeof(StringValueAttribute), false);
if (attributes.Length > 0)
{
var attribute = (StringValueAttribute) attributes[0];
StringValues.Add(value, attribute);
output = attribute.Value;
}
}
return output;
}
Tôi làm theo các liên kết dưới đây và tôi thấy nó hữu ích. [Trong C#, deserializing enums từ số nguyên.] (Http://stackoverflow.com/questions/9944421/c-sharp-deserializing-enums-from-integers/33387055#33387055) –