Compile time only attribute for source generators #4732
-
Compile time only attribute for source generatorsI've been experimenting with source generators and it works great. Only I'm not happy with how to trigger a source generator. Most examples use an attribute to indicate where source should be generated. In my experiments I also used comments: //# ValueStruct: double,json,comparable,plusminus
public partial struct Celcius
{
private const string PostFix = "°C";
public bool IsValid => Value >= -273.15d;
} And the source generator than generates the code for a single value struct with json conversion, validation, + and - operators, comparers and string parsing. Using comments is technically easy using I don't want to use attributes because they get emitted in to the resulting binary which results in unnecessary bytes spend and leaks some implementation details. Therefor I would like to propose attributes that are available during compile time to source generators but don't get emmitted in the resulting binary. Possible syntaxes: [[MyAttribute]] // using double brackets to indicate a non emitting attribute
public MyAttribute : NoEmitAttribute // using a special base class
[NoEmit] public MyAttribute : Attribute // using an attribute on the attribute Every usage of the attribute in actual code, outside the source generator, should generate a compiler error, like when calling Or perhaps a different implementation is possible like: sourcegenerator(sourcegeneratoridentifier, param1, param2, .... paramx)
{
public partial struct Celcius
{
private const string PostFix = "°C";
public bool IsValid => Value >= -273.15d;
}
} Where |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This has been discussed elsewhere (#4715), and the solution is to apply |
Beta Was this translation helpful? Give feedback.
-
This was also discussed in 2015. |
Beta Was this translation helpful? Give feedback.
This was also discussed in 2015.