2010-11-20 148 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。

相关问题