How can I qualify a .NET type with assembly name for Visual Studio debugger to disambiguate while using an ambiguous type?

前端 未结 3 1891
天命终不由人
天命终不由人 2021-02-20 10:30

I am using the VS debuggers \'Immediate Window\' to call a static API on a class which is an ambiguous type defined in 2 different assemblies.

The call fails with the fo

3条回答
  •  故里飘歌
    2021-02-20 11:08

    If you can not live with solution by jaredpar You may want to take a look at this SO question: How to disambiguate type in watch window when there are two types with the same name

    This approach can also be taken for Immediate Window with some limitation though. You are depending on where the debugger is currently stopped (think yellow arrow in left margin of editor) it seem it must be at a location where the alias has been used and are still in scope.

    Example:

    • Create ClassLibrary2
    • Create ClassLibrary3
    • Create ConsoleApplication1

    • Add ClassLibrary2 as reference to ConsoleApplication1 and change the property Aliases from global to myAlias2

    • Add ClassLibrary3 as reference to ConsoleApplication1 and change the property Aliases from global to myAlias3

    • change the content of the following files:

    Program.cs:

    namespace ConsoleApplication2
    {
        extern alias myAlias2;
        extern alias myAlias3;
        using myConsole2 = myAlias2::ClassLibrary.Console;
        using myConsole3 = myAlias3::ClassLibrary.Console;
        class Program
        {
            static void Main(string[] args)
            { // from now on you can use myAlias2::ClassLibrary.Console.Write("ABC") in Immediate Window
                myConsole2.Write("ABC");
                Write3();
                // from now on you can use myAlias2::ClassLibrary.Console.Write("ABC") in Immediate Window
            }
    
            private static void Write3()
            { // in here you can use both aliases
                myConsole3.Write("ABC");
            }
        }
    }
    

    ClassLibrary2/Class1.cs:

    namespace ClassLibrary
    {
        public static class Console
        {
            public static void Write(string text)
            { // in here You cannot use the aliases in Immediate Window
                System.Console.Write("===");
                System.Console.Write(text);
                System.Console.Write("===");
            }
        }
    }
    

    ClassLibrary3/Class1.cs:

    namespace ClassLibrary
    {
        public static class Console
        {
            public static void Write(string text)
            { // in here You cannot use the aliases in Immediate Window
                System.Console.Write("---");
                System.Console.Write(text);
                System.Console.Write("---");
            }
        }
    }
    

    Tested in VS2015 Community Edition

提交回复
热议问题