/// <summary> /// Registers error codes and names based on the given type, which can either be an enum or /// a class type with public static fields of type HResult. Each of the fields of the type /// are registered as error codes with the corresponding names, and when HResult objects /// with those codes are asked for detailed messages, the given resource manager will be /// used to look up a string with the name of the field as the key. /// </summary> /// <param name="type">Either an enum type, or a class type containing public static HResult fields</param> /// <param name="resourceManager">Resource manager used to look up detailed messages for these error codes</param> public static void RegisterErrorCodes(Type type, ResourceManager resourceManager) { lock (lockObject) { if (errorCodes == null) { errorCodes = new Dictionary <int, ErrorCodeData>(); } if (type.IsEnum) { string[] enumNames = Enum.GetNames(type); Array enumValues = Enum.GetValues(type); Debug.Assert(enumNames.Length == enumValues.Length); for (int i = 0; i < enumNames.Length; i++) { int code = (int)enumValues.GetValue(i); errorCodes[code] = new ErrorCodeData { Code = code, Message = resourceManager == null ? "" : null, Name = enumNames[i], ResourceManager = resourceManager }; } } else { FieldInfo[] errorCodeFields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); foreach (var f in errorCodeFields) { if (f.FieldType == typeof(HResult)) { int code = ((HResult)f.GetValue(null)).value; errorCodes[code] = new ErrorCodeData { Code = code, Message = resourceManager == null ? "" : null, Name = f.Name, ResourceManager = resourceManager }; } } } } }
/// <summary> /// Registers error codes and names based on the given type, which can either be an enum or /// a class type with public static fields of type HResult. Each of the fields of the type /// are registered as error codes with the corresponding names, and when HResult objects /// with those codes are asked for detailed messages, the given resource manager will be /// used to look up a string with the name of the field as the key. /// </summary> /// <param name="type">Either an enum type, or a class type containing public static HResult fields</param> /// <param name="resourceManager">Resource manager used to look up detailed messages for these error codes</param> public static void RegisterErrorCodes(Type type, ResourceManager resourceManager) { lock (lockObject) { if (errorCodes == null) { errorCodes = new Dictionary<int, ErrorCodeData>(); } if (type.IsEnum) { string[] enumNames = Enum.GetNames(type); Array enumValues = Enum.GetValues(type); Debug.Assert(enumNames.Length == enumValues.Length); for (int i = 0; i < enumNames.Length; i++) { int code = (int)enumValues.GetValue(i); errorCodes[code] = new ErrorCodeData { Code = code, Message = resourceManager == null ? "" : null, Name = enumNames[i], ResourceManager = resourceManager }; } } else { FieldInfo[] errorCodeFields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); foreach (var f in errorCodeFields) { if (f.FieldType == typeof(HResult)) { int code = ((HResult)f.GetValue(null)).value; errorCodes[code] = new ErrorCodeData { Code = code, Message = resourceManager == null ? "" : null, Name = f.Name, ResourceManager = resourceManager }; } } } } }