/// <summary> /// Initializes a new instance of the <see cref="T:LocalizedEnumConverterDetailsAttribute"/> class. /// </summary> /// <param name="assemblyName">The name of the assembly which contains the resource manager.</param> /// <param name="resourceManagerName">The name of the <see cref="ResourceManager"/> to use.</param> /// <param name="namingPattern">The <see cref="LocalizedEnumNamingPattern"/> to use.</param> public LocalizedEnumConverterDetailsAttribute(string assemblyName, string resourceManagerName, LocalizedEnumNamingPattern namingPattern) { if (resourceManagerName == null) { throw new ArgumentNullException("resourceManagerName"); } if (resourceManagerName.Length == 0) { throw new ArgumentException("resourceManagerName", "The resourceManagerName cannot be empty."); } _AssemblyName = assemblyName; _ResourceManagerName = resourceManagerName; _NamingPattern = namingPattern; }
/// <summary> /// Initializes a new instance of the <see cref="LocalizedEnumConverter"/> class. /// </summary> /// <param name="type">The type.</param> /// <param name="resourceManager">The resource manager to use to get translated enumeration names.</param> public LocalizedEnumConverter( Type type, ResourceManager resourceManager ) : base( type ) { if( resourceManager == null ) { // 1. Check if the Type has a LocalizedEnumConverterDetailsAttribute object[] attributes = type.GetCustomAttributes( typeof( LocalizedEnumConverterDetailsAttribute ), true ); if( attributes.Length > 0 ) { LocalizedEnumConverterDetailsAttribute detailsAttribute = attributes[ 0 ] as LocalizedEnumConverterDetailsAttribute; if( detailsAttribute != null ) { _NamingPattern = detailsAttribute.NamingPattern; if( detailsAttribute.AssemblyName == null ) { _ResourceManager = new ResourceManager( detailsAttribute.ResourceManagerName, type.Assembly ); } else { _ResourceManager = new ResourceManager( detailsAttribute.ResourceManagerName, Assembly.Load( detailsAttribute.AssemblyName ) ); } } } else { // Try to find a ResourceManager for this enumeration, get one named with the enumeration Type's name, // then one named Enumerations and finally one named Strings. Assembly assembly = type.Assembly; string[] resourceNames = assembly.GetManifestResourceNames(); foreach( string resourceName in resourceNames ) { string baseName = resourceName.Replace( ".resources", "" ); if( baseName.EndsWith( "." + type.Name ) ) { _ResourceManager = new ResourceManager( baseName, type.Assembly ); } else if( baseName.EndsWith( ".Enumerations" ) ) { _ResourceManager = new ResourceManager( baseName, type.Assembly ); } else if( baseName.EndsWith( "Strings" ) ) { _ResourceManager = new ResourceManager( baseName, type.Assembly ); } } } } else { _ResourceManager = resourceManager; } object[] flagAttributes = type.GetCustomAttributes( typeof( FlagsAttribute ), true ); _IsFlagEnum = flagAttributes.Length > 0; if( _IsFlagEnum ) { _FlagValues = Enum.GetValues( type ); } }
/// <summary> /// Initializes a new instance of the <see cref="LocalizedEnumConverter"/> class. /// </summary> /// <param name="type">The type.</param> /// <param name="resourceManager">The resource manager to use to get translated enumeration names.</param> public LocalizedEnumConverter(Type type, ResourceManager resourceManager) : base(type) { if (resourceManager == null) { // 1. Check if the Type has a LocalizedEnumConverterDetailsAttribute object[] attributes = type.GetCustomAttributes(typeof(LocalizedEnumConverterDetailsAttribute), true); if (attributes.Length > 0) { LocalizedEnumConverterDetailsAttribute detailsAttribute = attributes[0] as LocalizedEnumConverterDetailsAttribute; if (detailsAttribute != null) { _NamingPattern = detailsAttribute.NamingPattern; if (detailsAttribute.AssemblyName == null) { _ResourceManager = new ResourceManager(detailsAttribute.ResourceManagerName, type.Assembly); } else { _ResourceManager = new ResourceManager( detailsAttribute.ResourceManagerName, Assembly.Load(detailsAttribute.AssemblyName)); } } } else { // Try to find a ResourceManager for this enumeration, get one named with the enumeration Type's name, // then one named Enumerations and finally one named Strings. Assembly assembly = type.Assembly; string[] resourceNames = assembly.GetManifestResourceNames(); foreach (string resourceName in resourceNames) { string baseName = resourceName.Replace(".resources", ""); if (baseName.EndsWith("." + type.Name)) { _ResourceManager = new ResourceManager(baseName, type.Assembly); } else if (baseName.EndsWith(".Enumerations")) { _ResourceManager = new ResourceManager(baseName, type.Assembly); } else if (baseName.EndsWith("Strings")) { _ResourceManager = new ResourceManager(baseName, type.Assembly); } } } } else { _ResourceManager = resourceManager; } object[] flagAttributes = type.GetCustomAttributes(typeof(FlagsAttribute), true); _IsFlagEnum = flagAttributes.Length > 0; if (_IsFlagEnum) { _FlagValues = Enum.GetValues(type); } }