C
C#2y ago
MrScautHD

✅ Attribute that can remove Methods and class from compiling

Hi, im looking for a attribute that can remove methods and classes from the compiler, or would it possbile to doing something like that by my self?
21 Replies
Buddy
Buddy2y ago
In what contect? You can use preprocessors if you for example want something only during debug mode. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#conditional-compilation
MrScautHD
MrScautHDOP2y ago
i need it for like a attribute so like
[Headless(true)] .... (Method get removed)
[Headless(true)] .... (Method get removed)
Buddy
Buddy2y ago
No such attribute Only conditional preprocessor is the go-to for that.
MrScautHD
MrScautHDOP2y ago
so you mean the #if
Buddy
Buddy2y ago
Yes.
FestivalDelGelato
there are also partial methods that are removed if not implemented
MrScautHD
MrScautHDOP2y ago
attibute like contional would cool
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
MrScautHD
MrScautHDOP2y ago
no
SinFluxx
SinFluxx2y ago
What else are you waiting for?
FestivalDelGelato
stephen toub to implement selective methods :p
MrScautHD
MrScautHDOP2y ago
Is there no way to impliment this?
SinFluxx
SinFluxx2y ago
You got the answer above I believe, conditional preprocessors?
MrScautHD
MrScautHDOP2y ago
Would IT possible to do a Attribute for it
SinFluxx
SinFluxx2y ago
There's: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=net-7.0 But it only works for void methods So unless that fits the bill for all methods you're trying to exclude, probably easier to just go with the original suggestion Is there a reason you "need" it to be an attribute?
MrScautHD
MrScautHDOP2y ago
I Just want a Clean system Because IT get often used And allot of #if would look at the time very messy and a simple Attribute could clean that nothing that works for classes and all methods (Functions)
SinFluxx
SinFluxx2y ago
Not that I'm aware of, think you're asking for something that doesn't exist
Buddy
Buddy2y ago
I've mentioned what exists. Source generators can't rewrite source, only add to it. So that's out of the question. Conditional Preprocessor is the only way. You can write an external program that analyzes the code and automatically adds a conditional preprocessor, but that'll be too much work for such little gain. Just use Conditional preprocessor.
MrScautHD
MrScautHDOP2y ago
:/ What about decompile it Thats messy to right
Buddy
Buddy2y ago
Why? No.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?