2013-11-15 45 views
1

我正在開發的擴展,VS2013之一。因爲它會通過MSI安裝,我使用ProvideBindingPath屬性打包類變更基本目錄來安裝文件夾。但是在運行時加載的第三方dll引用並不是從探測路徑中選取dll。它總是看着Visual Studio devenv.exe文件夾。有沒有辦法強制DLL查看我的安裝文件夾。VSIX擴展使用第三方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

是否已經'ProvideBindingPath'屬性被移動到另一個的名稱空間;在VS2012中,此類型位於'Microsoft.VisualStudio.Shell'之前。我會嘗試不初始化'SubPath'屬性。並且:彙編元數據是隻讀的;我會說設置反射屬性上的SubPath屬性沒有任何影響... – Matze

+0

Matze,ProvideBindingPath屬性沒有移動到任何其他名稱空間,它來自建模類。 [鏈接](http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.modeling.shell.providebindingpathattribute(v=vs.120).aspx)這是工作的罰款與VS2010。問題在我將代碼遷移到2013年時開始。同一段代碼無法運行。 –

+0

我解決了使用LoadLibrary()從System.Runtime.InteropServices問題; 因爲我要加載的DLL是COM iterop。 public static class win32 { [DllImport(「kernel32.dll」)] public static extern IntPtr LoadLibrary(string dllToLoad); [的DllImport( 「KERNEL32.DLL」)] 公共靜態外部的IntPtr GetProcAddress的(IntPtr的HMODULE,串過程名); [的DllImport( 「KERNEL32.DLL」)] 公共靜態的extern BOOL FreeLibrary則(IntPtr的HMODULE); } in package.cs我寫了這個 win32.LoadLibrary(Path.Combine(installationPath,「apidsp_windows.dll」)); –

回答

2

我已經能夠使用下面的代碼在我的擴展中成功加載第三方(telerik)程序集。

註冊到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; 
} 

我還沒有與上述方法的任何問題。

+0

謝謝,Utkarsh,在我的情況的DLL不會在執行路徑可用。微星將把它們放在安裝文件夾中。 –

1

我決定使用 LoadLibrary()問題從

System.Runtime.InteropServices; 

,因爲我的dll被加載是一個COM DLL iterop。

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")); 
+0

你把上面的代碼放在哪裏('[DLLImport(...')? –