首页 新闻 会员 周边

C#: Program 使用 top-level statements 如何指定 namespace

0
悬赏园豆:30 [已解决问题] 解决于 2023-02-13 15:55

在 Program 中写上 namespace 语句,之后的代码全部报错

问题补充:

默认情况下 namespace 为空,可以通过下面的代码验证

Console.WriteLine("namespace: " + MethodBase.GetCurrentMethod().DeclaringType.Namespace);

指定命名空间是为了根据命名空间设置日志级别(LogLevel)

dudu的主页 dudu | 高人七级 | 园豆:31003
提问于:2023-02-13 11:58
< >
分享
最佳答案
0

通过 How to add a namespace using the new dotnet 6 template? 的回答知道了答案:无法指定 namespace

Adding namespaces outside top-level statements is not supported.

  1. Top-level statements are implicitly in the global namespace.

  2. Top-level statements are specifically not allowed in combination with file-scoped namespace declarations (introduced in C# 10 supported by .NET 6).

dudu | 高人七级 |园豆:31003 | 2023-02-13 15:47

接着发现 LogLevel 是根据程序集的名称而不是 namespace

<AssemblyName>Cnblogs.Zzk.Api</AssemblyName>
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Cnblogs.Zzk.Api": "Warning"
    }
  }
}
dudu | 园豆:31003 (高人七级) | 2023-02-13 15:57

尝试了在 Program.cs 中添加 partial class,也无济于事

namespace Cnblogs.Zzk.Api
{
    public partial class Program
    { }
}
dudu | 园豆:31003 (高人七级) | 2023-02-14 12:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册