/// <summary> /// Converts a Windows constant integer value to a human-readable /// string, e.g. 0x0007 to WM_SETFOCUS. /// </summary> /// <typeparam name="T">The clr type of the native constant.</typeparam> /// <param name="value">The value to convert to a string.</param> /// <param name="constantType">The native constant "type".</param> /// <returns>A string representation of the native constant value.</returns> public static string ConvertToString <T>(T value, NativeConstantType constantType) { Type type = typeof(T); // Get the collection of maps for the given type. // Return the int value as string if the collection doesn't exist. Dictionary <NativeConstantType, NativeConstantMap <T> > mapCollection = null; if (_maps.ContainsKey(type)) { mapCollection = (Dictionary <NativeConstantType, NativeConstantMap <T> >)_maps[type]; } else { return(value.ToString()); } // Get the map for the constant type. // Return the int value as string if the collection doesn't exist. Dictionary <T, string> map = null; if (mapCollection.ContainsKey(constantType)) { map = mapCollection[constantType]; } else { return(value.ToString()); } // Return the mapped string. // Return the int value as string if the collection doesn't exist. if (map.ContainsKey(value)) { return(map[value]); } else { return(value.ToString()); } }
// Creates a new Constant-to-String map for the given NativeConstantType. // This may be called just once for each NativeConstantType. private static void _CreateMap <T>(NativeConstantType constantType, string prefix) { Dictionary <NativeConstantType, NativeConstantMap <T> > mapCollection = null; Type type = typeof(T); if (!_maps.ContainsKey(type)) { mapCollection = new Dictionary <NativeConstantType, NativeConstantMap <T> >(); _maps.Add(type, mapCollection); } else { mapCollection = (Dictionary <NativeConstantType, NativeConstantMap <T> >)_maps[type]; } if (mapCollection.ContainsKey(constantType)) { throw new ArgumentException("A map for constant type '" + constantType.ToString() + "' has already been created."); } // Add new NativeConstantMap to map collection. mapCollection.Add(constantType, new NativeConstantMap <T>(prefix)); }
/// <summary> /// Converts a Windows constant integer value to a human-readable /// string, e.g. 0x0007 to WM_SETFOCUS. /// </summary> /// <param name="value">The value to convert to a string.</param> /// <param name="constantType">The native constant "type".</param> public static string ConvertToString(int value, NativeConstantType constantType) { return(ConvertToString <int>(value, constantType)); }