2010-11-20 150 views
1

我正在爲我需要加載程序集並在不同的appdomain中執行它的桌面應用程序。無法找到類型或名稱空間名稱

對於加載組件我已經寫爲:

public static DataTable GetAllPluginNames(string[] args) 
{ 
     SqlConnection sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); 

     //ToDo: create a table of one column - only name of the plugin and return that. 
     //ToDo: refer the code from MFAssemblyValidator from MFPluggerService. 

     DataTable dt = null; 
     List<string> assemblyNames = new List<string>(); 
     Assembly[] oAssemblies = new Assembly[args.Length]; 

     for (int assemblyCount = 0; assemblyCount < args.Length; assemblyCount++) 
     { 
      oAssemblies[assemblyCount] = Assembly.LoadFile(args[assemblyCount]); 

      try 
      { 
       foreach (Type oType in oAssemblies[assemblyCount].GetTypes()) 
       { 
        // Check whether class is inheriting from IMFDBAnalyserPlugin. 
        if (oType.GetInterface("IMFDBAnalyserPlugin") == typeof(IMFDBAnalyserPlugin)) 
        { 
         assemblyNames.Add(args[assemblyCount].Substring(args[assemblyCount].LastIndexOf("\\") + 1)); 
        } 
       } 
       return dt; 
      } 
      catch (Exception ex) 
      { 
       lblError.Text = "ERROR"; 
      } 


     // Passing data one application domain to another. 
     AppDomain.CurrentDomain.SetData("AssemblyNames", assemblyNames.ToArray()); 
     } 
} 

typeof(IMFDBAnalyserPlugin))是表示一個命名空間錯誤。

IMFDBAnalyserPlugin是我作爲程序的接口類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace MFDBAnalyser 
{ 
    public interface IMFDBAnalyserPlugin 
    { 
     void ExecutePlugin(); 
    } 
} 

可能是什麼問題? 任何人都可以幫助我!

回答

1

是在GetAllPluginNames方法而定位在相同namespace作爲接口IMFDBAnalyserPlugin

如果沒有,你要麼需要一個using directive添加到包含GetAllPluginNames方法的代碼文件的頂部,或者完全限定其命名空間的接口引用,即

if (oType.GetInterface("MFDBAnalyser.IMFDBAnalyserPlugin") == typeof(MFDBAnalyser.IMFDBAnalyserPlugin)) 
+0

它不工作太...... – Srivastava 2010-11-20 09:46:47

+0

每次你在你的代碼中使用它時,你都需要提供'IMFDBAnalyserPlugin' *的全限定引用,除非你選擇在代碼文件的頂部。我用完整的語法更新了我的文章。 – 2010-11-20 09:51:15

0

嘗試typeof(MFDBAnalyser.IMFDBAnalyserPlugin)

4

快速解決方案一: 在項目屬性中,將Dotnet框架從2.0,3.0或3.5更改爲4,編譯並運行!

快速解決方案II: 檢查.cs屬性 - 從內容更改爲編譯。

更多詳細信息可以參考here

1

這完全讓我困惑了一段時間。當我嘗試編譯該項目時,我添加了引用和代碼,但它莫名其妙地失去了引用的知識,同時仍在解決方案資源管理器中顯示它們。

最後,我導航到的項目屬性,改變theb「目標框架」字段從「.Net框架4客戶端配置文件」到」 .Net框架4'

這個固定的ISSE。

相關問題