Using extension methods in the [DebuggerDisplay] attribute

The [DebuggerDisplay] attribute ( Using DebuggerDisplayAttribute ) allows you to define the display in the VS 2010/2008 debugger. By modifying AutoExp.cs / .dll, I can even override the display of system types and third-party types, for example.

[assembly: DebuggerDisplay (@"\{Name = {Name} FullName = {FullName}}", Target = typeof (Type))] 

In inner braces, I can refer to fields, properties, and methods. Can I refer to extension methods ?

As an example, I tried to display shorter type names, for example. $SCG.Dictionary instead of System.Collections.Generic.Dictionary . I added this to AutoExp.cs:

 using DbgDisp; [assembly: DebuggerDisplay (@"\{Name = {Name} ShortName = {ShortName()}}", Target = typeof (Type))] namespace DbgDisp { public static class Ext { public static string ShortName (this Type t) { return string.Format ("[{0}]", t.Name); } } // Ext } // DbgDisp 

but the debugger complains: the name "ShortName" does not exist in the current context.

Am I missing something, or is it just impossible to use extension methods there?

I know that I can override ToString () , but this only helps for my own types.

+6
source share
3 answers

In short, no. For the same reasons that extension methods do not work with dynamic , that is, only from the method name, there is no way to find out which using directives worked, and therefore which extension methods are candidates. It is entirely possible to have scenarios in which using various using directives modifies the available methods, so there is no use trying to guess.

You will have to restrict yourself to regular methods, unless the line allows you to explicitly specify static methods in classes, i.e. DbgDisp.Ext.ShortName(foo) .

+4
source

You can actually use extension methods by passing this as an argument

 [assembly: DebuggerDisplay(@"NetGuid = {ToString()} OracleGuid = {GuidExtensions.ToVarChar(this)}", Target = typeof(Guid))] public static class GuidExtensions { public static string ToVarChar(this Guid guid) { var newBytes = new byte[16]; var oldBytes = guid.ToByteArray(); for (var i = 8; i < 16; i++) newBytes[i] = oldBytes[i]; newBytes[3] = oldBytes[0]; newBytes[2] = oldBytes[1]; newBytes[1] = oldBytes[2]; newBytes[0] = oldBytes[3]; newBytes[5] = oldBytes[4]; newBytes[4] = oldBytes[5]; newBytes[6] = oldBytes[7]; newBytes[7] = oldBytes[6]; return new Guid(newBytes).ToString("N").ToUpper(); } } 
+3
source

You can put a private method in your class that uses the extension method that you want to generate the string. The DebuggerDisplay attribute can reference this method.

0
source

Source: https://habr.com/ru/post/885556/


All Articles