我現在正在爲AssenblyResolve事件苦苦掙扎。我搜索了stackoverflow,並做了其他Google搜索,並嘗試了所有我認爲相關的內容。這裏有被越接近我的問題(在我看來)鏈接:AssemblyResolve未被依賴關係解僱
我有靜態方法的引導程序類(我將刪除我們有的線程安全代碼,只是爲了清晰起見:
public static void Initialize()
{
AppDomain.CurrentDomain.AssemblyResolve += CustomResolve;
}
private static Assembly CustomResolve(object sender, ResolveEventArgs args)
{
// There is a lot code here but basicall what it does.
// Is determining which architecture the computer is running on and
// extract the correct embedded dll (x86 or x64). The code was based
// on milang on GitHub (https://github.com/milang/P4.net). And it's the same
// purpose we want to be able to load the x86 or x64 version of the perforce dll
// but this time with the officially Perforce supported p4api.net.
// Once the dll is extracted we assign it to the boostrapper
Bootstrapper._p4dnAssembly = Assembly.LoadFile(targetFileName);
// Make sure we can satisfy the requested reference with the embedded assembly (now extracted).
AssemblyName reference = new AssemblyName(args.Name);
if (AssemblyName.ReferenceMatchesDefinition(reference, Bootstrapper._p4dnAssembly.GetName()))
{
return Bootstrapper._p4dnAssembly;
}
}
我能夠使代碼工作,如果我有一個主要方法和靜態構造函數的簡單類。靜態構造函數只是簡單地調用Boostrapper.Initialize()方法。 之後,我可以用我的圖書館,它按預期工作:
public static class Test
{
static Test()
{
Bootstrapper.Initialize();
}
public static void Main()
{
// Using the library here is working fine. The AssemblyResolve event was
// fired (confirmed by a breakpoint in Visual Studio)
}
}
我的問題是,如果有相關性至少一層。基本上,代碼保持不變,但是這一次我的圖書館的代碼是另一個庫中:
public static class Test
{
static Test()
{
Bootstrapper.Initialize();
}
public static void Main()
{
Class1 myClass = new Class1();
// The following line is using the code of the extracted library, but
// The AssemblyResolve event is not fired (or fired before I register the
// callback) and therefore the library is not found : result
// BadImageFormatException() error could not load libary because one
myClass.Connect();
}
}
聽起來#我前面提到的鏈接2解釋我所看到的,但它不工作。 AssemblyResove回調上的Visual Studio斷點永遠不會被擊中。
想知道發生了什麼?
弗朗西斯