简体   繁体   中英

Namespace and class same as the .NET Framework NameSpace and class

I am completely aware that we can have same namespace and the .NET Framework Namespace, and how to access the .NET framework namespace class, when the custom namespace is hiding it.

My question is if I have a custom namespace and class name same as .NET framework namespace and class name, how can I access the .NET framework namespace and class inside it? Example: If I create a C# project with System namespace and a class Console with two methods Read() and Write() , how will I be able to access the .NET framework namespace and console to display output..

Apologies for asking such type of question.. It my curiosity to know what happens in such cases.

Thanks

I tried using the extern alias method and actually couldn't get it to work. There is no way to do what you are asking for ( Cunningham's Law ).

As a workaround, you could just redirect the call as I have done below:

namespace System
{
    extern alias ActualSystem;
    //using SC = ActualSystem::System.Console;  
    //does not work:
    //Error 1   The type or namespace name 'Console' does not exist in the namespace 'ActualSystem::System' 
    //          (are you missing an assembly reference?)    
    using ClassLibrary;

    public static class Console
    {
        public static void Write(string s)
        {
            ConsoleRedirect.Write(s);
        }
    }
}

namespace ClassLibrary
{
    using System;
    public static class ConsoleRedirect
    {
        public static void Write(string s)
        {
            Console.Write(s);
        }
    }
}

You can use external aliases in Visual Studio. Here is the description of the command line parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM