简体   繁体   English

命名空间和类与.NET Framework命名空间和类相同

[英]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. 我完全知道,自定义名称空间隐藏了名称空间和.NET Framework名称空间,以及如何访问.NET Framework名称空间类,我们可以使用相同的名称空间。

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? 我的问题是,如果我具有与.NET Framework名称空间和类名称相同的自定义名称空间和类名称,如何访问其中的.NET Framework名称空间和类? 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.. 示例:如果我创建一个具有System命名空间的C#项目和一个具有两个方法Read()Write()的类Console ,那么我将如何访问.NET Framework名称空间和控制台以显示输出。

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. 我尝试使用extern别名方法,但实际上无法正常工作。 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. 您可以在Visual Studio中使用外部别名 Here is the description of the command line parameters. 是命令行参数的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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