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

前端 未结 3 1956
天命终不由人
天命终不由人 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 10:57

    As Maslow suggests, it is possible to use reflection to get what you want. It's not pretty, though. For example, if you want to see the value of the static property My.Properties.Settings.Default, then assuming that MainWindow is some other type in the assembly (e.g. blah.dll) that contains the value you want to debug, this expression will get you the value:

    System.Reflection.Assembly
        .GetAssembly(typeof(MainWindow))
        .GetType("My.Properties.Settings")
        .GetProperty("Default")
        .GetValue(null)
    

    In this example, My.Properties.Settings is a type that is defined in two different assemblies.

    It's also possible to call methods etc. by using the appropriate tools from the System.Reflection namespace.

    0 讨论(0)
  • 2021-02-20 11:02

    It sounds like you have two types which have the same name and namespace but live in different assemblies? If that is the case, unfortunately there is no way to disambiguate this call in the immediate window. The immediate window considers both of these types to be in scope and since assembly name cannot be a part of the cast syntax in C# or VB.Net there is no way to disambiguate these types.

    The only option you have is to create an alternate debug only API which binds to one or the other. Then call this during the debugging session.

    0 讨论(0)
  • 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 <code>myAlias2::ClassLibrary.Console.Write("ABC")</code> in Immediate Window
                myConsole2.Write("ABC");
                Write3();
                // from now on you can use <code>myAlias2::ClassLibrary.Console.Write("ABC")</code> 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

    0 讨论(0)
提交回复
热议问题