W moim ciągu zapytania mam zmienną wieku ?age=New_Born
.
Czy istnieje sposób, aby sprawdzić, czy ta wartość ciągu New_Born
znajduje się na mojej liście Enum
[Flags]
public enum Age
{
New_Born = 1,
Toddler = 2,
Preschool = 4,
Kindergarten = 8
}
Mógłbym teraz użyć instrukcji if, ale jeśli moja lista Enum się powiększy. Chcę znaleźć lepszy sposób, aby to zrobić. Zastanawiam się nad użyciem Linq, tylko nie wiem, jak to zrobić.
Enum.IsDefined
nie w porządku?Odpowiedzi:
Możesz użyć:
Enum.IsDefined(typeof(Age), youragevariable)
źródło
Enum.IsDefined()
rozróżniana jest wielkość liter! Nie jest to więc „uniwersalne rozwiązanie”.Możesz użyć metody Enum.TryParse:
Age age; if (Enum.TryParse<Age>("New_Born", out age)) { // You now have the value in age }
źródło
Możesz użyć metody TryParse , która zwraca wartość true, jeśli się powiedzie:
Age age; if(Enum.TryParse<Age>("myString", out age)) { //Here you can use age }
źródło
Mam poręczną metodę rozszerzającą, która używa TryParse, ponieważ IsDefined rozróżnia wielkość liter.
public static bool IsParsable<T>(this string value) where T : struct { return Enum.TryParse<T>(value, true, out _); }
źródło
Aby osiągnąć swój cel, powinieneś użyć Enum.TryParse
Oto przykład:
[Flags] private enum TestEnum { Value1 = 1, Value2 = 2 } static void Main(string[] args) { var enumName = "Value1"; TestEnum enumValue; if (!TestEnum.TryParse(enumName, out enumValue)) { throw new Exception("Wrong enum value"); } // enumValue contains parsed value }
źródło
Wiem, że jest to stary wątek, ale tutaj jest nieco inne podejście wykorzystujące atrybuty w wyliczeniach, a następnie klasa pomocnicza, aby znaleźć wyliczenie, które pasuje.
W ten sposób można mieć wiele mapowań na jednym wyliczeniu.
public enum Age { [Metadata("Value", "New_Born")] [Metadata("Value", "NewBorn")] New_Born = 1, [Metadata("Value", "Toddler")] Toddler = 2, [Metadata("Value", "Preschool")] Preschool = 4, [Metadata("Value", "Kindergarten")] Kindergarten = 8 }
Z taką klasą pomocników
public static class MetadataHelper { public static string GetFirstValueFromMetaDataAttribute<T>(this T value, string metaDataDescription) { return GetValueFromMetaDataAttribute(value, metaDataDescription).FirstOrDefault(); } private static IEnumerable<string> GetValueFromMetaDataAttribute<T>(T value, string metaDataDescription) { var attribs = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof (MetadataAttribute), true); return attribs.Any() ? (from p in (MetadataAttribute[]) attribs where p.Description.ToLower() == metaDataDescription.ToLower() select p.MetaData).ToList() : new List<string>(); } public static List<T> GetEnumeratesByMetaData<T>(string metadataDescription, string value) { return typeof (T).GetEnumValues().Cast<T>().Where( enumerate => GetValueFromMetaDataAttribute(enumerate, metadataDescription).Any( p => p.ToLower() == value.ToLower())).ToList(); } public static List<T> GetNotEnumeratesByMetaData<T>(string metadataDescription, string value) { return typeof (T).GetEnumValues().Cast<T>().Where( enumerate => GetValueFromMetaDataAttribute(enumerate, metadataDescription).All( p => p.ToLower() != value.ToLower())).ToList(); } }
możesz wtedy zrobić coś takiego
var enumerates = MetadataHelper.GetEnumeratesByMetaData<Age>("Value", "New_Born");
A dla kompletności jest tutaj atrybut:
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)] public class MetadataAttribute : Attribute { public MetadataAttribute(string description, string metaData = "") { Description = description; MetaData = metaData; } public string Description { get; set; } public string MetaData { get; set; } }
źródło
Aby przeanalizować wiek:
Age age; if (Enum.TryParse(typeof(Age), "New_Born", out age)) MessageBox.Show("Defined"); // Defined for "New_Born, 1, 4 , 8, 12"
Aby sprawdzić, czy jest zdefiniowane:
if (Enum.IsDefined(typeof(Age), "New_Born")) MessageBox.Show("Defined");
W zależności od tego, jak planujesz używać
Age
wyliczenia, flagi mogą nie być właściwe. Jak zapewne wiesz,[Flags]
oznacza, że chcesz zezwolić na wiele wartości (jak w masce bitowej).IsDefined
zwróci false,Age.Toddler | Age.Preschool
ponieważ ma wiele wartości.źródło