-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Support different attribute "retention" levels #4896
Comments
I'm not sure I understand the purpose of "assembly only" attributes. DecimalConstantAttribute is needed at runtime for late bound code compilation and debugging. |
What exactly do you mean by "runtime reflection" here? Is reading metadata of the assembly a runtime reflection? |
@tmat If you're reading the metadata bits out of an assembly's file on disk, that is not reflection but file I/O. If you're using reflection APIs to access attributes of currently loaded program elements, that is reflection. |
Love the idea! Source only attributes + Roslyn can unleash some metaprogramming magic! |
@gafter I don't think we should make Reflection produce different results than metadata reader. |
Source-only attributes already exist today with the help of |
Would "assembly only" attributes be readable through reflection-only assemblies or would you need to be reading through the assembly manually? I assume that such attributes would require CLR changes to support so that the CLR doesn't bother trying to read them at runtime. As for "source only" attributes, would that potentially include lifting some of the restrictions on what kind of parameters can be used since you wouldn't be limited to what the CLR can read/load at runtime? I could see some very interesting meta-programming scenarios enabled by supporting richer parameter types, like expression trees. The |
@HaloFour No, it doesn't apply to methods only. For source-only, this proposal is adding a new attribute that works like |
@MrJul Ah, must've been looking at an older definition of the attribute which used to only target methods. Either way, this doesn't accomplish the same task. By marking an attribute (or any class) with |
@HaloFour I think you're mistaken, the scenario you're talking about is specifically the one enabled by The JetBrains.Annotations NuGet package is a perfect example: all attributes are used for source analysis by JetBrains' ReSharper, but all usages are stripped from your source code thanks to Note: I'm not advocating against this proposal (who am I to criticize @gafter?). I'm simply pointing out the fact that this can already be done today, at least for source only attributes. Regarding the assembly only attribute, I agree with @tmat here: reflection should also be able to see them. If the aim is to hide some "compiler only" attributes from reflection, we might as well hide all those compiler-generated classes for closures or iterators from reflection. |
@MrJul Clearly I've not used that feature before. So yeah, you can sorta emulate "source only" attributes today as long as the program doesn't define whatever symbol decorates the attribute. I wonder if those attributes are visible to analyzers in Roslyn today. It's not a part of this spec as written but I would like to see some of the limitations regarding what kind of types can be used for attribute parameters is lifted some since the CLR isn't expected to be instantiating the attribute instances at run-time. |
It can get confusing quite quickly unless IDE starts actually supporting custom attributes #711.
Does this mean the new feature will potentially be applied to existing .NET Framework attributes? |
Attributes are currently available at runtime through reflection. However, we propose a meta-attribute (an attribute on attribute definitions) that the compiler and runtime would respect to control how long a use of the attribute is retained for.
SuppressMessageAttribute
.DecimalConstantAttribute
The retention levels lower than runtime may be useful for some meta-programming techniques.
/cc @mattwar @joeduffy @jaredpar @srivatsn @stephentoub @MadsTorgersen @JohnHamby @cston
The text was updated successfully, but these errors were encountered: