wasSharp – Diff between revs 19 and 21
?pathlinks?
Rev 19 | Rev 21 | |||
---|---|---|---|---|
Line 113... | Line 113... | |||
113 | /// <summary> |
113 | /// <summary> |
|
114 | /// Get enumeration value from its name attribute. |
114 | /// Get enumeration value from its name attribute. |
|
115 | /// </summary> |
115 | /// </summary> |
|
116 | /// <typeparam name="T">the enumeration type</typeparam> |
116 | /// <typeparam name="T">the enumeration type</typeparam> |
|
117 | /// <param name="name">the description of a member</param> |
117 | /// <param name="name">the description of a member</param> |
|
- | 118 | /// <param name="comparison">the string comparison to use</param> |
||
118 | /// <returns>the value or the default of T if case no name attribute found</returns> |
119 | /// <returns>the value or the default of T if case no name attribute found</returns> |
|
119 | public static T GetEnumValueFromName<T>(string name) |
120 | public static T GetEnumValueFromName<T>(string name, StringComparison comparison = StringComparison.OrdinalIgnoreCase) |
|
120 | { |
121 | { |
|
121 | var field = typeof(T).GetRuntimeFields().ToArray() |
122 | var field = typeof(T).GetRuntimeFields().ToArray() |
|
122 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
123 | .AsParallel().SelectMany(f => f.GetCustomAttributes( |
|
123 | typeof(NameAttribute), false), ( |
124 | typeof(NameAttribute), false), ( |
|
124 | f, a) => new {Field = f, Att = a}) |
125 | f, a) => new {Field = f, Att = a}) |
|
125 | .SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att) |
126 | .SingleOrDefault(a => Strings.StringEquals(((NameAttribute) a.Att) |
|
126 | .Name, name, StringComparison.Ordinal)); |
127 | .Name, name, comparison)); |
|
127 | return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
128 | return field != null ? (T) field.Field.GetValue(Activator.CreateInstance<T>()) : default(T); |
|
128 | } |
129 | } |
|
Line 129... | Line 130... | |||
129 | |
130 | |
|
130 | /////////////////////////////////////////////////////////////////////////// |
131 | /////////////////////////////////////////////////////////////////////////// |