C#别名属性(例如内联提示) [英] C# Alias an Attribute (such as Inline Hinting)

查看:458
本文介绍了C#别名属性(例如内联提示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想从一种荒谬的角度来缩短(非-使用-污染)内联属性:

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]

[InlineHint] [MyCompilerSvcs.InlineHint] 或类似内容-键入速度更快,也更容易进行心理分析。

I've been wanting for a while to shorten the (no-using-pollution) "inline" attribute from the absurdity that is:
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
to, well, [InlineHint] or [MyCompilerSvcs.InlineHint] or similar- something both quicker to type and quicker to mentally parse.

有什么实际方法吗?现在,我唯一看到的理智选项是使用System.Runtime.CompilerServices; 添加(在处理ASP.NET背后的代码时很麻烦网站),使用别名(甚至更糟)添加更具体的,或将其长格式保存在可访问的位置以进行复制粘贴。

Is there any way to actually do that? Right now the only "sane" options I can see are to either add using System.Runtime.CompilerServices; (which is a pain when dealing with code behind of an ASP.NET website), adding more specific using aliases (even worse), or to keep it long-form in an accessible location for copy-paste.

提供 2009年以来的问题不太过时,使用似乎是缩短属性引用的大小的唯一方法(因为没有人建议大型多文件项目使用类似关键字的变体)。 此相关问题来自2010年,并且还建议使用 技巧。

Providing 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 was from 2010, and also suggests a using trick.

从2015年开始,有问题,但它涉及的是装饰物。由于我感兴趣的是编译器指令本身(以及基于编译器的指令!),我怀疑运行时IL Emit可以做到这一点,并且代码桥不会很自然地扩展到我的编译器服务中

From 2015, there was this question, but it was in reference to the resulting decorations. Since what I'm interested in is the compiler directives themselves (and a performance-based one at that!) I doubt a runtime IL Emit could do this, and a "code bridge" doesn't quite naturally extend to compiler services in my mind.

当前针对C#4.5,但不允许更高版本。

Currently targeting C# 4.5, but newer versions are not forbidden.

在编译器执行自动内联!,它只会在32个IL字节或更少的时间内这样做,并且内联提示会覆盖大小限制。还有其他一些可能更有用的选项,如NoOptimization,NoInline和Synchronized,它们非常有用,我非常希望不必使用使用语句。

And before "The compiler does inlining automatically!", it only does so for 32 IL Bytes or less and the inline hint overrides the size restriction. There are also other options which could be useful to have more accessible, such as NoOptimization, NoInline, and Synchronized, all of which I would very much like to not have to type absurdly long attributes to access without using statements.

推荐答案

您可以编写基于Roslyn 的工具来执行此操作。这样就可以应用具有您选择的名称的属性(某些短名称,例如 AggInline ),该工具将发出实际的 AggressiveInlining 属性和必需的使用指令。您可以看到 ImmutableObjectGraph 工具,作为在Roslyn中执行类似操作的示例。

You can write a 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 tool as an example on how to do something like that in Roslyn.

这篇关于C#别名属性(例如内联提示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆