找不到类型或命名空间,但它就在那里

时间:2014-03-18 17:00:00

标签: c# visual-studio-2012 namespaces scope

我创建了一个包含公共类的公共类的dll。

我在新项目中添加了dll作为参考,我试图从dll的类中创建一个新对象

using myDll;

namespace foo
{
    class bar
    {
        static void Main(string[] args)
        {
            myDll.myClass test = new myDll.myClass();
            test.myVoidMethod();
            [...]

但是当我尝试使用test时,visual studio说

The type or namespace name 'test' could not be found (are you missing a using directive or an assembly reference?)

这是范围问题吗?

mydll code(摘录):

using System;

namespace myDll
{

    public class myClass
    {
       public static void myVoidMethod()
       {
           Console.Write("Hello");
       }
    }
}

3 个答案:

答案 0 :(得分:1)

问题:您不应该使用实例引用变量来加入static方法。

解决方案:您应该使用static

来调用classname方法

试试这个:

myDll.myClass.myVoidMethod();

编辑:来自您的评论but i want to create a new object

您的错误不是来自创建实例变量的第一行,而是来自使用实例变量调用static方法的第二个语句。

你仍然可以为你的班级创建一个机会

试试这个:

myDll.myClass test = new myDll.myClass(); //it works
myDll.myClass.myVoidMethod();

答案 1 :(得分:0)

您是否将属性Build Action放在了班级源文件的Compile上了?

答案 2 :(得分:0)

检查以确保您的项目未设置为使用.NET Framework 4 Client Profile。 (如下面的链接告诉我们):Namespace not recognized (even though it is there)。这可能对你很有帮助。