我正在开发VS2013的扩展。由于它将通过MSI安装,我使用 ProvideBindingPath 属性将基目录更改为安装文件夹到包类。但是,将在运行时加载的第三方dll引用不会从探测路径中选取dll。它一直在寻找Visual studio devenv.exe 文件夹。有没有办法强制dll查看我的安装文件夹。
using MD=Microsoft.VisualStudio.Modeling.Shell;
MD.ProvideBindingPath(SubPath = @"")]
public sealed class AutomationCommandPanePackage : Package
{
public AutomationCommandPanePackage()
{
string installationPath = HelperMethods.GetInstallationPath();
if (string.IsNullOrEmpty(HelperMethods.GetInstallationPath())) return;
// Change default config file at runtime.
using (AutomationConfigurationManager.Change(installationPath, "AutomationPro.config"))
{
// Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
}
Assembly a = Assembly.GetExecutingAssembly();
Type type = a.GetType("AutomationCommandPanePackage", true);
System.Reflection.MemberInfo info = type;
var attributes = info.GetCustomAttributes(true);
foreach (var attrib in attributes)
{
if (attrib is MD.ProvideBindingPathAttribute)
{
((MD.ProvideBindingPathAttribute)attrib).SubPath = installationPath;
break;
}
}
答案 0 :(得分:2)
我已经能够使用下面的代码在我的扩展程序中成功加载第三方(telerik)程序集。
在您的Package类构造函数
中注册到AssemblyResolve
事件
AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
然后在处理程序加载包中,如下所示:
string path = Assembly.GetExecutingAssembly().Location;
path = Path.GetDirectoryName(path);
if (args.Name.ToLower().Contains("telerik.windows.controls.gridview"))
{
path = Path.Combine(path, "telerik.windows.controls.gridview.dll");
Assembly ret = Assembly.LoadFrom(path);
return ret;
}
我对上述方法没有任何疑问。
答案 1 :(得分:1)
我解决了问题 来自
的LoadLibrary()
System.Runtime.InteropServices;
因为我要加载的dll是一个COM iterop dll。
public static class win32
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); [DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}
在package.cs中的我像这样加载了程序集
win32.LoadLibrary(Path.Combine(installationPath, "apidsp_windows.dll"));