【问题标题】:Type or namespace name could not be found找不到类型或命名空间名称
【发布时间】:2011-05-13 00:49:46
【问题描述】:

我正在开发一个桌面应用程序,我需要为其加载程序集并在不同的 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();
    }
}

可能是什么问题? 谁能帮帮我!!

【问题讨论】:

    标签: c# visual-studio namespaces


    【解决方案1】:

    快速解决方案一: 在项目属性中,将Dotnet框架从2.0、3.0或3.5改为4,编译运行!

    快速解决方案二: 检查 .cs 属性 - 从内容更改为编译。

    更多详情请见here

    【讨论】:

      【解决方案2】:

      GetAllPluginNames方法是否与接口IMFDBAnalyserPlugin位于同一namespace下?

      如果没有,您需要在包含GetAllPluginNames 方法的代码文件顶部添加using directive,或者使用其命名空间完全限定接口引用,即

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

      【讨论】:

      • 您需要为IMFDBAnalyserPlugin 每次在您的代码中使用它提供完全限定的参考,除非您选择在以下位置添加using 指令代码文件的顶部。我用完整的语法更新了我的帖子。
      【解决方案3】:

      这让我困惑了一段时间。我添加了引用和代码,然后当我尝试编译项目时,它莫名其妙地丢失了对引用的了解,同时仍在解决方案资源管理器中显示它们。

      最后,我导航到项目属性并将“目标框架”字段从“.Net Framework 4 Client Profile”更改为“.Net Framework 4”

      这解决了问题。

      【讨论】:

        【解决方案4】:

        试试typeof(MFDBAnalyser.IMFDBAnalyserPlugin)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-12
          • 2013-03-25
          • 2012-06-19
          • 2017-11-29
          • 2012-09-27
          • 2011-05-06
          • 2013-01-07
          相关资源
          最近更新 更多