How can I qualify a .NET type with assembly name for Visual Studio debugger to disambiguate while using an ambiguous type?
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 following message:
The type foo
exists in both blah.dll
and bar.dll
This message makes sense since this is indeed the case. My question is how can I work around this in the debugger to specify which assembly I want to be used for binding this type?
Is there a way to qualify a type with the assembly name that it is defined in?
Thanks Bhavin.
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.
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.
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
精彩评论