silikonwashington.blogg.se

Strukt dictionary
Strukt dictionary










I really like Jakub Šturc's answer, but it's shortcoming is that you cannot use it with a switch-case statement.Markieren Sie die Strukturobjekte für alle Zäune: Zaun- Struktur, Tor-Links- Struktur, Tor-Rechts- Struktur und Zaun-sml- Struktur.

#Strukt dictionary code#

This code nicely caters for enums where you don't need a "Friendly name" and will return just the. If we have no description attribute, just return the ToString of the enum Return ((DescriptionAttribute)attrs).Description Object attrs = memberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false) If (memberInfo != null & memberInfo.Length > 0) MemberInfo memberInfo = type.GetMember(enumerationValue.ToString()) Tries to find a DescriptionAttribute for a potential friendly name Throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue")

strukt dictionary

Simply decorate the enum and then use this code to retrieve it: public static string GetDescription(this object enumerationValue) I use the Description attribute from the System.ComponentModel namespace. You could then access it easily straight from your enum instance: Console.WriteLine(())

strukt dictionary

StringValue attrs = fi.GetCustomAttributes(typeof(StringValue), false) as StringValue public static string GetStringValue(this AuthenticationMethod value) You could take your solution one step further and make your GetStringValue an extension method of your enum and then you wouldn't need to reference it like StringEnum.GetStringValue. I do like your original solution though for more complex scenarios though. Of course, it doesn't allow you to have a different friendly name than your enum which your solution does provide. That saves running all the way around the houses creating custom attributes and attaching them to your enums or using lookup tables to marry an enum value with a friendly string and best of all it's self managing and can be used on any Pascal Case string which is infinitely more reusable. Which can easily be called from any string: Console.WriteLine("ConvertM圜razyPascalCaseSentenceToFriendl圜ase".ToFriendl圜ase()) Ĭonvert My Crazy Pascal Case Sentence To Friendly Case and if you name your enums in Pascal Case (as I do - such as ThisIsMyEnumValue = 1 etc.) then you could use a very simple regex to print the friendly form: static string ToFriendl圜ase(this string EnumString) public sealed class AuthenticationMethod ", ()) I also tried something with a dictionary and static properties but that wasn't better either. I was wondering if there is a better solution for this.

strukt dictionary

Okay now all of these work like a charm but I find it a whole lot of work. I can then use it like this: string valueOfAuthenticationMethod = StringEnum.GetStringValue(AuthenticationMethod.FORMS) Good now I've got the tools to get a string value for an enumerator. Public static string GetStringValue(Enum value)įieldInfo fi = type.GetField(value.ToString()) įi.GetCustomAttributes(typeof(StringValue),

strukt dictionary

Then I can add this attribute to my enumerator: public enum AuthenticationMethodĪnd of course I need something to retrieve that StringValue: public static class StringEnum I have found the following solution for this problem ( link):įirst I need to create a custom attribute called "StringValue": public class StringValue : System.Attribute The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id 1. I have the following enumeration: public enum AuthenticationMethod










Strukt dictionary