简体   繁体   English

C#在专案中使用类别

[英]C# Using Classes in Project

I have organised my project with separate folders for groups of classes, but now in order to get to any method I have to reference the whole path like: 我已经用单独的文件夹为类组组织了我的项目,但是现在为了使用任何方法,我必须引用整个路径,例如:

            Classes.Users.UsersClass.Get();
            Classes.Database.ConnectionClass.Test();
            if (!Classes.Database.UsersMethods.Authenticate())
            {
                Classes.Users.UsersClass.LoginFailed();
            }

As you can see, this is going to get messy after a while, so is there a way I can just call the class directly? 如您所见,一段时间后这会变得混乱,有没有办法我可以直接调用该类呢?


/Edit /编辑

This is the fixed up version: 这是固定版本:

    Users.GetWindowsUser();
    Connection.Test();
    if (!UserMethods.Authenticate())
    {
        Users.LoginFailed();
    }

You can add a using directives at the top of your C# file: 您可以在C#文件的顶部添加using指令

using Classes.Users;
using Classes.Database;

This would then let you type: 然后,您可以输入:

UserClass.Get();
ConnectionClass.Test();

That being said, I would strongly recommend not using "Class" as a suffix on every class, and also recommend not using a namespace named "Classes". 话虽这么说,我强烈建议不要在每个类上都使用“ Class”作为后缀,并且也建议不要使用名为“ Classes”的命名空间。 Most things in C# are classes - there is no need to suffix every class with this in terms of naming. C#中的大多数东西都是类-无需在命名后为每个类加上后缀。

For details, please refer to the Namespace Naming and Class Naming guidelines on MSDN. 有关详细信息,请参阅MSDN上的命名空间命名类命名准则。

Add the appropriate using statement to the top of your file. 在文件顶部添加适当的using语句。 Eg using Classes.Database; 例如使用Classes.Database;

Also, in VS 2010 if you just type the name of the class without the namespace (eg ConnectionClass) then hit ctrl+. 同样,在VS 2010中,如果您只键入不带名称空间的类的名称(例如ConnectionClass),则请按ctrl +。 it will give you the option to automatically add the appropriate using statement. 它会为您提供自动添加适当的using语句的选项。

you can simply put using directives on the top of the file, or 您可以简单地使用指令放在文件顶部, 或者
if you don't want the classes to be in separated namespaces go to the class file and change the namespace to project original namesapce 如果您不希望这些类位于单独的命名空间中,请转到类文件并更改命名空间以投影原始名称

namespace myProject.SubFolder
{
.......
}

will be 将会

namespace myProject
{
.........
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM