簡體   English   中英

VS2019 奇怪的 nuget 命名空間解析

[英]Weird nuget namespace resolution by VS2019

我一直在解決我在 Visual Studio 中遇到的一些奇怪行為。 我正在使用簡單的Silo.Configuration命名空間生成包,其中在一個文件中有兩種類型。

  1. 文件內容:
namespace Silo.Configuration
{
    public enum ClusteringType
    {
        Local,
        Zookeeper
    }

    public class SiloConfig
    {
        public string URL { get; set; }

        public string ClusterId { get; set; }

        public string ServiceId { get; set; }

        public ClusteringType ClusteringType { get; set; }

        public int GatewayPort { get; set; }

        public int SiloPort { get; set; }
    }

}
  1. Silo.Configuration是我通過 Visual Studio 使用<GeneratePackageOnBuild>true</GeneratePackageOnBuild>選項創建的 package 的默認命名空間和程序集名稱。

到目前為止一切順利,但是當我嘗試在嵌套在其他命名空間內的其他項目中使用這個 package 時,我得到了 CS0234 當我在全局命名空間中移動Silo.Configuration或添加 using global:: 時,奇怪的事情發生了,這修復了錯誤並解決了類型。

問題是這種行為是什么,我該如何解決? 在您回答之前,我注意到當我能夠從另一個項目中解析SiloConfig時,我希望將其包含在內,它看起來像這樣

using Silo.Configuration; // <-- notice this?

namespace Silo.Configuration
{
    public class SiloConfig
    {
        public SiloConfig();

        public string URL { get; set; }
        public string ClusterId { get; set; }
        public string ServiceId { get; set; }
        public ClusteringType ClusteringType { get; set; }
        public int GatewayPort { get; set; }
        public int SiloPort { get; set; }
    }
}

如果命名空間相同,在SiloConfig類型之上使用什么會生成此內容?

使用package時,需要先從本地導入package。

這個錯誤CS0234是你沒有導入嗎?

關於 global:: 的用處,可以參考這個鏈接: What is global::? , 在同一個命名空間的情況下,通過給命名空間中的類起別名,代碼可讀性更高。

這是示例:使用 newname=global::Silo.Configuration; If you want to use the class with the same name in the namespace with the same name, this will cause the class in the imported package to conflict with the class in the currently used project.

導入的class沒有效果,所以只能在當前項目中更改class名稱。 這樣兩個類都可以使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM