在 [DebuggerDisplay] 属性中使用扩展方法
属性 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上你可以使用扩展方法将其作为参数传递
Actually you can use extension methods passing this as the argument
简而言之,不。出于同样的原因,扩展方法不适用于动态,即仅从方法名称来看,无法知道
使用什么
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 whatusing
directives were in effect, and hence which extension methods are candidates. It is entirely possible to have scenarios where using differentusing
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.