C# 名称空间未被识别?

C# 名称空间未被识别?,c#,visual-studio-2010,C#,Visual Studio 2010,我在VisualStudio(2010)中创建了一个非常简单的域层。然后,我使用newtest向导创建了一个基本单元测试。但是,当我尝试输入using语句以便测试代码时。。它说找不到我的命名空间。。。这是我第一次使用VisualStudio,所以我不知道自己做错了什么 我的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Home { cl

我在VisualStudio(2010)中创建了一个非常简单的域层。然后,我使用newtest向导创建了一个基本单元测试。但是,当我尝试输入using语句以便测试代码时。。它说找不到我的命名空间。。。这是我第一次使用VisualStudio,所以我不知道自己做错了什么

我的代码

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

namespace Home
{
    class InventoryType
    {

        /// <summary>
        /// Selects the inventory type and returns the selected value
        /// </summary>
        public class InventorySelect
        {
            private string inventoryTypes;
            public String InventoryTypes
            {
                set
                {
                    inventoryTypes = value;
                }

                get
                {
                    return inventoryTypes;
                }
            }


            /// <summary>
            /// Validate that the inventory is returning some sort of value
            /// </summary>
            /// <returns></returns>
            public bool Validate()
            {
                if (InventoryTypes == null) return false;
                return true;
            }
        }
    }
}

InventoryType
类声明为
public


InventorySelect
类可以是
private
而不是
public
声明
InventoryType
类为
public


InventorySelect
类可以是
private
而不是
public
使用引用命名空间,而不是特定类(除非为类名添加别名)。你的using语句应该只包含Home这个词

using Home.InventoryType.InventorySelect; 
//becomes
using Home;

这里有一个指向MSDN on using指令的链接:

using指的是一个名称空间,而不是一个特定的类(除非为类名添加别名)。你的using语句应该只包含Home这个词

using Home.InventoryType.InventorySelect; 
//becomes
using Home;

这里有一个指向MSDN的链接,指向using指令:

我假设您的测试类位于它自己的项目中,所以您需要添加对该项目的引用。(using语句不添加引用,它只允许您在代码中使用类型,而不完全限定其名称。)

我假设您的测试类位于它自己的项目中,因此您需要添加对该项目的引用。(using语句不添加引用,它只允许您在代码中使用类型,而不完全限定其名称。)

当您在解决方案中创建“多项目”时(通过将项目添加到任何现有解决方案中),项目之间互不了解

转到解决方案资源管理器上的测试项目,在“引用”下,右键单击并选择“添加引用”。然后选择“项目”选项卡,您将能够将项目的引用添加到测试项目中

另外,确保将项目中的类定义为“public”,以便能够在测试项目中访问它们

namespace Home
{
    public class InventoryType
    {
            ...
    }
}
请注意,您仍然需要在C#test类的顶部添加“using”关键字:

using Home;

namespace HomeTest
{
    public class TestInventoryTypeCase
    {
           ...
    }
}
在解决方案中创建“多项目”(通过将项目添加到任何现有解决方案中)时,这些项目彼此不了解

转到解决方案资源管理器上的测试项目,在“引用”下,右键单击并选择“添加引用”。然后选择“项目”选项卡,您将能够将项目的引用添加到测试项目中

另外,确保将项目中的类定义为“public”,以便能够在测试项目中访问它们

namespace Home
{
    public class InventoryType
    {
            ...
    }
}
请注意,您仍然需要在C#test类的顶部添加“using”关键字:

using Home;

namespace HomeTest
{
    public class TestInventoryTypeCase
    {
           ...
    }
}

好的,但这并没有改变这样一个事实,即我的测试类无法识别我的主命名空间,使我无法运行它?好的,但这并没有改变这样一个事实,即我的测试类无法识别我的主命名空间,使我无法运行它?if(Validate()=true)可以更简单地写成if(Validate())if(Validate()=true)可以更简单地编写,就好像(Validate())修复了它无法找到using语句一样,谢谢。但是它仍然找不到validate或InventorySelect等?validate()是InventorySelect类上的一个方法,因此就validate()而言,您不能单独调用该方法。您已使用“InventorySelect=new InventorySelect();”行实例化了一个InventorySelect,因此需要使用“select.Validate()”。这修复了无法找到using语句的问题,谢谢。但是它仍然找不到validate或InventorySelect等?validate()是InventorySelect类上的一个方法,因此就validate()而言,您不能单独调用该方法。您已经用“InventorySelect=newinventoryselect();”行实例化了一个InventorySelect,因此需要使用“select.Validate()”。