è possibile analizzare il valore di attributo XML di nuovo ad un tipo enum con:
var value = Enum.Parse(typeof(Fuel), "B");
Ma non credo che si otterrà davvero lontano con i propri valori "speciali" (3
, a/
ecc). Perché non si fa a definire il tuo enum come
enum Fuel
{
Benzine,
Diesel,
// ...
Three,
ASlash,
// ...
}
e scriviamo un metodo statico per convertire una stringa in un membro enum?
Una cosa che è possibile esaminare per l'implementazione di tale metodo consiste nell'aggiungere attributi personalizzati ai membri dell'enumerazione contenente la loro rappresentazione di stringa: se un valore non ha una controparte esatta nell'enumerazione, cercare un membro con attributo.
La creazione di un tale attributo è facile:
/// <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;
}
}
E poi si possono usare nel vostro enum:
enum Fuel
{
[StringValue("B")]
Benzine,
[StringValue("D")]
Diesel,
// ...
[StringValue("3")]
Three,
[StringValue("/")]
Slash,
// ...
}
Questi due metodi vi aiuterà analizzare una stringa in un membro enum della vostra scelta:
/// <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;
}
E mentre io sono a esso: qui è il contrario;)
/// <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;
}
seguo sul link qui sotto e lo trovo utile. [In C#, deserializzazione delle enumerazioni dagli interi.] (Http://stackoverflow.com/questions/9944421/c-sharp-deserializing-enums-from-integers/33387055#33387055) –