MillKa MillKa - 1 year ago 174
C# Question

Using extension methods in [DebuggerDisplay] Attribute

The Attribute [DebuggerDisplay] (Using DebuggerDisplayAttribute) allows to define the display in the Debugger of VS 2010/2008. By modifying AutoExp.cs/.dll, I can even override the display of system types and 3rd party types, e.g.

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

In the inner curly braces I can reference fields, properties and methods. Is it possible to reference extension methods ?

As an example, I tried to display shorter type names, e.g.
instead of
. 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 not possible to use extension methods there ?

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

Answer Source

In short, no. For the same reasons that extension methods don't work with dynamic, which is that from just the method name, there is no way of knowing what using directives were in effect, and hence which extension methods are candidates. It is entirely possible to have scenarios where using different using directives changes the available methods, so there is no benefit in having it try to guess.

You'll have to limit yourself to regular methods, unless the string allows you to specify static methods on classes explicitly, i.e. DbgDisp.Ext.ShortName(foo).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download