using System; using System.Collections; using System.Globalization; using System.IO; using System.Resources; namespace Muchinfo.MTPClient.CustomException { public class ErrorResourceManager : ResourceManager { private const string _defaultFileName = "Muchinfo.ErrorResource.zh-CN.dll"; private const string _defaultCultureName = "zh-CN"; private const string _unKnownResource = "UnKnownResource"; private const string _unfindResource = "UnFindResource"; private static ResourceManager resourceMan; public static ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Muchinfo.MTPClient.CustomException.ErrorResources", typeof(ErrorResources).Assembly); resourceMan = temp; } return resourceMan; } } public ErrorResourceManager() { ResourceSets = new Hashtable(); var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _defaultFileName); try { LoadResourceFiles(filePath, _defaultCultureName); } catch (Exception) //读取不到文件使用系统默认的 { } } public ErrorResourceManager(string errorResourceFilePath) { ResourceSets = new Hashtable(); try { LoadResourceFiles(errorResourceFilePath, _defaultCultureName); } catch (Exception) //读取不到文件使用系统默认的 { } } public void LoadResourceFiles(string resourceFile, string cultureName) { var reader = new ResourceReader(resourceFile); var resourceSet = new ResourceSet(reader); this.ResourceSets[cultureName] = resourceSet; } public override string GetString(string name) { var culture = new CultureInfo(_defaultCultureName); return GetString(name, culture); } public override string GetString(string name, CultureInfo culture) { if (this.ResourceSets != null && this.ResourceSets.Count > 0) { if (culture == null) { culture = new CultureInfo(_defaultCultureName); } if (this.ResourceSets.Contains(culture.Name)) { var resourceSets = this.ResourceSets[culture.Name] as ResourceSet; if (resourceSets != null) { try { return resourceSets.GetString(name); } catch { return _unfindResource; } } } } else { return ResourceManager.GetString(name, null); } return _unKnownResource; } } public partial class ErrorResources { private static bool _resourceLoaded = false; public static bool ResourceLoaded { get { return _resourceLoaded; } } public static void Resource(ResourceManager manager) { resourceMan = manager; _resourceLoaded = true; } } }