private static bool TryGetEnumValue(Type resultType, string value, out object result, bool flagsEnumAllowed) { if (!resultType.IsEnum()) { result = null; return(false); } if (flagsEnumAllowed && resultType.IsDefined(_flagsAttribute.GetType(), false)) { ulong union = 0; foreach (string v in value.SplitAndTrimTokens(',')) { FieldInfo enumField = resultType.GetField(v, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public); if (enumField == null) { throw new NLogConfigurationException($"Invalid enumeration value '{value}'."); } union |= Convert.ToUInt64(enumField.GetValue(null), CultureInfo.InvariantCulture); } result = Convert.ChangeType(union, Enum.GetUnderlyingType(resultType), CultureInfo.InvariantCulture); result = Enum.ToObject(resultType, result); return(true); } else { FieldInfo enumField = resultType.GetField(value, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public); if (enumField == null) { throw new NLogConfigurationException($"Invalid enumeration value '{value}'."); } result = enumField.GetValue(null); return(true); } }
private static void ShowAttributes(MemberInfo attributeTarget) { Attribute[] attributes = Attribute.GetCustomAttributes(attributeTarget); Console.WriteLine("Attributes applied to {0}: {1}", attributeTarget.Name, (attributes.Length == 0 ? "None" : String.Empty)); foreach (Attribute attribute in attributes) { // Display the type of each applied attribute Console.WriteLine(" {0}", attribute.GetType().ToString()); DebuggerDisplayAttribute dda = attribute as DebuggerDisplayAttribute; if (dda != null) { Console.WriteLine(" Value={0}, Name={1}, Target={2}", dda.Value, dda.Name, dda.Target); } FlagsAttribute flags = attribute as FlagsAttribute; if (flags != null) { Console.WriteLine(flags.ToString()); Console.WriteLine(flags.GetType().Name); } } }