在 [DebuggerDisplay] 属性中使用扩展方法

发布于 2024-10-31 07:57:42 字数 960 浏览 7 评论 0原文

属性 [DebuggerDisplay](使用 DebuggerDisplayAttribute)允许定义显示VS 2010/2008 的调试器。通过修改AutoExp.cs/.dll,我什至可以覆盖系统类型和第3方类型的显示,例如

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

在内花括号中我可以引用字段、属性和方法。是否可以引用扩展方法

例如,我尝试显示较短的类型名称,例如 $SCG.Dictionary 而不是 System.Collections.Generic.Dictionary。我将其添加到 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

但调试器抱怨:名称“ShortName”在当前上下文中不存在。

我是否遗漏了某些内容,或者是否无法在那里使用扩展方法?

我知道我可以重写 ToString (),但这仅对我自己的类型有帮助。

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. $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 not possible to use extension methods there ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花细雨 2024-11-07 07:57:42

实际上你可以使用扩展方法将其作为参数传递

[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();
    }    
}

Actually you can use extension methods passing this as the 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();
    }    
}
无声无音无过去 2024-11-07 07:57:42

简而言之,不。出于同样的原因,扩展方法不适用于动态,即仅从方法名称来看,无法知道使用什么 code> 指令已生效,因此哪些扩展方法是候选方法。完全有可能出现使用不同的 using 指令更改可用方法的情况,因此让它尝试猜测没有任何好处。

您必须将自己限制为常规方法,除非该字符串允许您显式指定类上的静态方法,即 DbgDisp.Ext.ShortName(foo)

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).

您可以在类中放置一个私有方法,该方法使用您想要生成字符串的扩展方法。然后,DebuggerDisplay 属性可以引用该方法。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文