C# 别名属性(例如内联提示)
C# Alias an Attribute (such as Inline Hinting)
一段时间以来,我一直想缩短 (no-using
-pollution) "inline" 属性,因为荒谬的是:
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
好吧,[InlineHint]
或 [MyCompilerSvcs.InlineHint]
或类似的东西——打字速度和心理解析速度都更快。
有没有办法真正做到这一点?现在,我能看到的唯一 "sane" 选项是添加 using System.Runtime.CompilerServices;
(这在处理 ASP.NET 网站的代码隐藏时很痛苦),添加更具体的 using
别名(甚至更糟),或者将其以长格式保存在易于复制粘贴的可访问位置。
提供 this question from 2009 isn't too outdated, using
seems to be the only way to shorten how large the attribute reference is (as nobody suggested a more keyword-like variant for large, multifile projects). This related question 是从 2010 年开始的,并且还提出了一个 using
技巧。
从 2015 年开始,有 ,但它参考了由此产生的装饰。由于我感兴趣的是编译器指令本身(以及基于性能的指令!)我怀疑运行时 IL Emit 可以做到这一点,并且 "code bridge" 不会很自然地扩展到编译器服务我的想法。
目前针对 C# 4.5,但不禁止更新版本。
在 "The compiler does inlining automatically!" 之前,它只对 32 个 IL 字节或更少的字节执行此操作,并且内联提示会覆盖大小限制。还有其他选项可能有助于提高可访问性,例如 NoOptimization、NoInline 和 Synchronized,所有这些我都非常希望在没有 using
语句的情况下不必键入荒谬的长属性来访问。
您可以编写一个 Roslyn-based tool to do that. This enables to apply an attribute with a name of your choice (some short name such as AggInline
) and the tool will emit the actual AggressiveInlining
attribute and the required using
directives. You can see the ImmutableObjectGraph 工具作为示例,说明如何在 Roslyn 中执行类似的操作。
一段时间以来,我一直想缩短 (no-using
-pollution) "inline" 属性,因为荒谬的是:
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
好吧,[InlineHint]
或 [MyCompilerSvcs.InlineHint]
或类似的东西——打字速度和心理解析速度都更快。
有没有办法真正做到这一点?现在,我能看到的唯一 "sane" 选项是添加 using System.Runtime.CompilerServices;
(这在处理 ASP.NET 网站的代码隐藏时很痛苦),添加更具体的 using
别名(甚至更糟),或者将其以长格式保存在易于复制粘贴的可访问位置。
提供 this question from 2009 isn't too outdated, using
seems to be the only way to shorten how large the attribute reference is (as nobody suggested a more keyword-like variant for large, multifile projects). This related question 是从 2010 年开始的,并且还提出了一个 using
技巧。
从 2015 年开始,有
目前针对 C# 4.5,但不禁止更新版本。
在 "The compiler does inlining automatically!" 之前,它只对 32 个 IL 字节或更少的字节执行此操作,并且内联提示会覆盖大小限制。还有其他选项可能有助于提高可访问性,例如 NoOptimization、NoInline 和 Synchronized,所有这些我都非常希望在没有 using
语句的情况下不必键入荒谬的长属性来访问。
您可以编写一个 Roslyn-based tool to do that. This enables to apply an attribute with a name of your choice (some short name such as AggInline
) and the tool will emit the actual AggressiveInlining
attribute and the required using
directives. You can see the ImmutableObjectGraph 工具作为示例,说明如何在 Roslyn 中执行类似的操作。