Search code examples
c#namespacesnuget

How to set the namespace for NuGet package before publish it?


I have 2 NuGet packages: 1 main (YamatoDaiwaCS_Extensions) and 1 localization (OfficialLocalizations/Japanese; in the future, I am planning more localizations).

enter image description here

Why I have multiple projects in single the solution (and repository) is, once experiments done, all packages must be updated, versioned and published consistently. By other words, it is the monorepository.

If to publish the Japanese localization package as is, the classes from this library (e. g. JapanesePhoneNumber) will be imported from Japanese namespace. How to make this namespace YamatoDaiwaCS_Extensions.OfficialLocalizations.Japanese?

Examples of desired imports:

using YamatoDaiwaCS_Extensions; // Core package
using YamatoDaiwaCS_Extensions.DataMocking; // Core package
using YamatoDaiwaCS_Extensions.Japanese; // Localization package

Solution

  • Put Namespaces in Your Code

    namespace YamatoDaiwaCS_Extensions {
        // Your classes and code here
    }
    
    namespace YamatoDaiwaCS_Extensions.Japanese {
        // Your classes and code here
    }
    

    Ensure that the AssemblyInfo.cs file in your project contains the correct assembly information, including the assembly name.

    [assembly: AssemblyTitle("YourPackage")]
    [assembly: AssemblyDescription("Description of YourPackage")]
    [assembly: AssemblyConfiguration("")]
    [assembly: AssemblyCompany("YourCompany")]
    [assembly: AssemblyProduct("YourPackage")]
    [assembly: AssemblyCopyright("Copyright © YourCompany")]
    [assembly: AssemblyTrademark("")]
    [assembly: AssemblyCulture("")]
    

    Update your Project File (.csproj) (normal)

    <RootNamespace>YamatoDaiwaCS_Extensions</RootNamespace>
    

    Update your Project File (.csproj) (Japanese)

    <RootNamespace>YamatoDaiwaCS_Extensions.Japanese</RootNamespace>
    

    Package Configuration

    ensure that the generated .nupkg file contains the correct assembly information and structure. You can inspect the contents of the NuGet package using tools like NuGet Package Explorer.

    Last Step

    After verifying and setting up the appropriate configurations, you can use the nuget pack command to create the NuGet package. The namespace information will be inherent to the compiled code and project structure.

    nuget pack YamatoDaiwaCS_Extensions.csproj
    nuget pack Japanase.csproj