我正在使用VSIX项目,该项目是使用反射构建DLL的命令,我的目标DLL也会反射。
我用
加载目标DLL asmBase = System.IO.Path.GetDirectoryName(assemblyName);
var settings = System.Configuration.ConfigurationManager.ConnectionStrings[0];
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
asm = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(assemblyName));
这是我的AssemblyResolve事件
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
//This handler is called only when the common language runtime tries to bind to the assembly and fails.
//Retrieve the list of referenced assemblies in an array of AssemblyName.
Assembly MyAssembly, objExecutingAssemblies;
string strTempAssmbPath = "";
objExecutingAssemblies = args.RequestingAssembly;
AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
//Loop through the array of referenced assembly names.
foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
{
//Check for the assembly names that have raised the "AssemblyResolve" event.
if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
{
//Build the path of the assembly from where it has to be loaded.
strTempAssmbPath = asmBase + "\\" + args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
break;
}
}
//Load the assembly from the specified path.
MyAssembly = Assembly.LoadFrom(strTempAssmbPath);
//Return the loaded assembly.
return MyAssembly;
}
我的问题是当我的目标DLL执行这行代码时:
string cachePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
cachePath = Path.GetDirectoryName(cachePath);
GetExecutingAssembly
方法为我抛出"路径不是合法格式" 例外。
GetExecutingAssembly方法的预期路径结果是什么?
我尝试使用
System.Diagnostics.Debug.WriteLine(cachePath);
但它没有显示信息。