C# 使用静态 class 属性就好像它们被命名空间一样?

C# use static class properties as if they were namespaced?

我正在尝试创建一个 class 来保存我所有的全局常量,例如:

namespace MyProj
{
    public static class Constants
    {
        public const string MY_STRING = "this needs to be used ad nauseum";
    }
}

效果很好。但是,结果是在代码中我必须始终键入:

doSomethingWith(Constants.MY_STRING);

当我真正想做的是去(类似的事情)时:

using MyProj.Constants;
doSomethingWith(MY_STRING);

我怎样才能做到这一点?

static 添加到您的 using:

using static MyProj.Constants;

详情here.