NetEscapades.EnumGenerators
NetEscapades.EnumGenerators copied to clipboard
Is there any possible to generate the enum extensions when the enum is not defined in my project?
When I refrence a enum type from others package, I cannot add EnumExtensions Attribute to there Enum type.
Is there any possible to generate the enum extensions? just like the System.Text.Json Source Generator?
Like this: when user write this:
[EnumExtensions(typeof(OuterEnum))]
public static partial class OuterEnumExtension
{
}
The source generator will add code:
public static partial class OuterEnumExtension
{
public static string ToStringFast(OuterEnum e)
{
//...
}
//other method
}
That would actually be very useful, since existing enum types are being used in APIs that we intend to consume, and would rather avoid mapping the exact values to a custom enum just for the generated extensions. Your proposed method is pretty much flexible enough to contain the intended helpers into the static class of our choice, potentially avoiding unexpected conflicts.
This appears to be a duplicate of https://github.com/andrewlock/NetEscapades.EnumGenerators/issues/18. Source generators need access to the source to work. To make this work, the source generator would need to use reflection for the external enum. This assumes of course the reference is a nuget/assembly reference.
Source generators need access to the source to work. To make this work, the source generator would need to use reflection for the external enum. This assumes of course the reference is a nuget/assembly reference.
The type is still available to the generator to analyze. In Roslyn-powered applications like analyzers and source generators, the type information of a type is available via Microsoft.CodeAnalysis.ITypeSymbol instances, not System.Reflection.Type instances. ITypeSymbol could be a type defined in the same assembly that the generator runs on, or could be an externally available type from another assembly as referred to via project or package references. In other words, if you can type typeof(ExternalType), your generator can read information about ExternalType.
This means that this is very possible.
This appears to be a duplicate of #18.
As I read from the closing comment on the linked issue, it is mistakenly assumed that this is not possible. It might require a lot of rewriting in some areas, depending on the assumptions that were initially made, but this feature is something handy and is feasible, according to the above.
Thanks for the clarification and sorry for the incorrect conclusion. I was needing something like as well with generated nswag enumerations. I didn't want to have to have a custom template to add the attribute. The proposal above would help in this situation as well.
I could use this as well; we use enumerations defined in the project, but are the result of a tool which shouldn't be modified. Being able to do something like:
[assembly:EnumExtensions(typeof(SomeEnum))]
Would be very beneficial.