3 回答

TA貢獻1712條經(jīng)驗 獲得超3個贊
基本解決方案
遵循casperOne的建議,我終于能夠完成這項工作。
我將解決方案代碼放在問題中,因為casperOne提供了唯一的答案,我不想添加自己的答案。
通過將膽量從“ InternalGetResourceSet”方法中實現(xiàn)的Framework資源查找后備機制中剔除,并使我們的程序集搜索成為第一個使用的機制,我能夠使其工作。如果在當(dāng)前程序集中找不到該資源,則我們調(diào)用base方法來啟動默認的搜索機制(由于下面的@Wouter注釋)。
為此,我派生了“ ComponentResourceManager”類,并覆蓋了僅一個方法(并重新實現(xiàn)了私有框架方法):
class SingleAssemblyComponentResourceManager :
System.ComponentModel.ComponentResourceManager
{
private Type _contextTypeInfo;
private CultureInfo _neutralResourcesCulture;
public SingleAssemblyComponentResourceManager(Type t)
: base(t)
{
_contextTypeInfo = t;
}
protected override ResourceSet InternalGetResourceSet(CultureInfo culture,
bool createIfNotExists, bool tryParents)
{
ResourceSet rs = (ResourceSet)this.ResourceSets[culture];
if (rs == null)
{
Stream store = null;
string resourceFileName = null;
//lazy-load default language (without caring about duplicate assignment in race conditions, no harm done);
if (this._neutralResourcesCulture == null)
{
this._neutralResourcesCulture =
GetNeutralResourcesLanguage(this.MainAssembly);
}
// if we're asking for the default language, then ask for the
// invariant (non-specific) resources.
if (_neutralResourcesCulture.Equals(culture))
culture = CultureInfo.InvariantCulture;
resourceFileName = GetResourceFileName(culture);
store = this.MainAssembly.GetManifestResourceStream(
this._contextTypeInfo, resourceFileName);
//If we found the appropriate resources in the local assembly
if (store != null)
{
rs = new ResourceSet(store);
//save for later.
AddResourceSet(this.ResourceSets, culture, ref rs);
}
else
{
rs = base.InternalGetResourceSet(culture, createIfNotExists, tryParents);
}
}
return rs;
}
//private method in framework, had to be re-specified here.
private static void AddResourceSet(Hashtable localResourceSets,
CultureInfo culture, ref ResourceSet rs)
{
lock (localResourceSets)
{
ResourceSet objA = (ResourceSet)localResourceSets[culture];
if (objA != null)
{
if (!object.Equals(objA, rs))
{
rs.Dispose();
rs = objA;
}
}
else
{
localResourceSets.Add(culture, rs);
}
}
}
}
要實際使用此類,您需要在Visual Studio創(chuàng)建的“ XXX.Designer.cs”文件中替換System.ComponentModel.ComponentResourceManager-并且每次更改設(shè)計表單時都需要執(zhí)行此操作-Visual Studio會替換該類自動編碼。(該問題在“ 自定義Windows窗體設(shè)計器以使用MyResourceManager ”中進行了討論,但沒有找到更優(yōu)雅的解決方案-我在預(yù)構(gòu)建步驟中使用fart.exe來自動替換。)

TA貢獻1836條經(jīng)驗 獲得超3個贊
另一個快速說明-使用ILMerge困擾我的一件事是它是附加的專有Microsoft工具,默認情況下未隨Visual Studio一起安裝,因此存在額外的依賴關(guān)系,這使得第三方上手有點困難與我的開源項目。
我最近發(fā)現(xiàn)了ILRepack,它是一種等效于開源(Apache 2.0)的工具,到目前為止,它對我也可以正常工作(直接替換),并且可以隨項目源自由分發(fā)。
我希望這可以幫助某個人!
- 3 回答
- 0 關(guān)注
- 519 瀏覽
添加回答
舉報