如何避免在 class C# 之前编写命名空间
How to avoid having to write namespace before class C#
所以我制作了一个 .dll,并将其添加到我的项目中,一切正常,但是当我尝试使用我的 .dll 中的任何 class 时。我必须专门使用 namespace.classname 而不是只能说 Classname 即使我放在项目顶部
using namespace
using System;
using MyTestClassLibrary;
using System.IO;
using YangHandler;
namespace UsingMyclassdll
{
class Program
{
static void Main(string[] args)
{
YangHandler.YangHandler yangh = YangHandler.YangHandler.Parse("Rawtext");
Console.ReadKey();
}
}
}
在使用 Yanghandler 的那一行visual studio 说
Using directive is unnecessary
这不正是using用来使用其他命名空间的吗?
YangHandler code
using System;
using System.IO;
namespace YangHandler
{
public class YangHandler
{
public string YangAsRawText { get; private set; }
public static YangHandler Parse(string YangAsRawText)
{
YangHandler handlerToReturn = new YangHandler();
handlerToReturn.YangAsRawText = YangAsRawText;
return handlerToReturn;
}
我知道可以通过使用命名空间“UsingMyclassdll”下的命名空间别名来解决,例如
using YangHandler = YangHandler.YangHandler;
但是没有更正常的解决方案吗?
查看 Microsoft 的这篇非常有趣的文档:https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces
DO NOT use the same name for a namespace and a type in that namespace.
For example, do not use Debug as a namespace name and then also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.
因此,您的解决方法基本上是定义完全限定名称,因为类型和名称空间具有相同的名称。
没有解决办法。编译器不知道你指的是那个还是那个。
所以我制作了一个 .dll,并将其添加到我的项目中,一切正常,但是当我尝试使用我的 .dll 中的任何 class 时。我必须专门使用 namespace.classname 而不是只能说 Classname 即使我放在项目顶部
using namespace
using System;
using MyTestClassLibrary;
using System.IO;
using YangHandler;
namespace UsingMyclassdll
{
class Program
{
static void Main(string[] args)
{
YangHandler.YangHandler yangh = YangHandler.YangHandler.Parse("Rawtext");
Console.ReadKey();
}
}
}
在使用 Yanghandler 的那一行visual studio 说
Using directive is unnecessary
这不正是using用来使用其他命名空间的吗?
YangHandler code
using System;
using System.IO;
namespace YangHandler
{
public class YangHandler
{
public string YangAsRawText { get; private set; }
public static YangHandler Parse(string YangAsRawText)
{
YangHandler handlerToReturn = new YangHandler();
handlerToReturn.YangAsRawText = YangAsRawText;
return handlerToReturn;
}
我知道可以通过使用命名空间“UsingMyclassdll”下的命名空间别名来解决,例如
using YangHandler = YangHandler.YangHandler;
但是没有更正常的解决方案吗?
查看 Microsoft 的这篇非常有趣的文档:https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces
DO NOT use the same name for a namespace and a type in that namespace.
For example, do not use Debug as a namespace name and then also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.
因此,您的解决方法基本上是定义完全限定名称,因为类型和名称空间具有相同的名称。
没有解决办法。编译器不知道你指的是那个还是那个。