两个资源管理器使用一个作为另一个的后备
本文关键字:另一个 两个 资源管理器 一个 | 更新日期: 2023-09-27 17:53:38
假设我有两个resourcemanager:
var mgr1 = new ResourceManager("NS1.StringResources", Assembly.GetExecutingAssembly());
var mgr2 = new ResourceManager("NS2.OtherStringResources", _otherAssembly);
有办法可以合并它们吗?或者他们的资源集。这样我就可以有一个管理器或资源集
不好看,但你最终会在一个合并的字典:
var mgr1 = new ResourceManager("NS1.StringResources", Assembly.GetExecutingAssembly());
var mgr2 = new ResourceManager("NS2.OtherStringResources", _otherAssembly);
var combined = new Dictionary<string, object>();
ResourceSet resourceSet = mgr1.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet) {
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
combined.Add(resourceKey, resource);
}
resourceSet = mgr2.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet) {
string resourceKey = entry.Key.ToString();
object resource = entry.Value;
combined.Add(resourceKey, resource);
}
最后得到了类似的东西,因为字典似乎是唯一的方法。
public Dictionary<string, string> GetTotalResourceSet(CultureInfo culture)
{
Dictionary<string, string> set;
if (_resources.TryGetValue(culture.Name, out set))
return set;
var wl = getWhitelabelResourceSet(culture);
var loc = getLocalResourceSet(culture);
var dict = loc.Cast<DictionaryEntry>()
.ToDictionary(x => { return x.Key.ToString(); }, x =>
{
if (x.Value.GetType() == typeof(string))
return x.Value.ToString();
return "";
});
if (wl != null)
{
var wlDict = wl.Cast<DictionaryEntry>()
.ToDictionary(x => { return x.Key.ToString(); }, x =>
{
if (x.Value.GetType() == typeof(string))
return x.Value.ToString();
return "";
});
set = wlDict.Concat(dict.Where(kvp => !wlDict.ContainsKey(kvp.Key))).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
else
{
set = dict;
}
if (set != null)
{
_resources.TryAdd(culture.Name, set);
}
return set;
}