C
C#15mo 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
Buddy15mo 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
MrScautHDOP15mo ago
i need it for like a attribute so like
[Headless(true)] .... (Method get removed)
[Headless(true)] .... (Method get removed)
Buddy
Buddy15mo ago
No such attribute Only conditional preprocessor is the go-to for that.
MrScautHD
MrScautHDOP15mo ago
so you mean the #if
Buddy
Buddy15mo ago
Yes.
Omnissiah
Omnissiah15mo ago
there are also partial methods that are removed if not implemented
MrScautHD
MrScautHDOP15mo ago
attibute like contional would cool
Accord
Accord15mo 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
MrScautHDOP15mo ago
no
SinFluxx
SinFluxx15mo ago
What else are you waiting for?
Omnissiah
Omnissiah15mo ago
stephen toub to implement selective methods :p
MrScautHD
MrScautHDOP15mo ago
Is there no way to impliment this?
SinFluxx
SinFluxx15mo ago
You got the answer above I believe, conditional preprocessors?
MrScautHD
MrScautHDOP15mo ago
Would IT possible to do a Attribute for it
SinFluxx
SinFluxx15mo 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
MrScautHDOP15mo 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
SinFluxx15mo ago
Not that I'm aware of, think you're asking for something that doesn't exist
Buddy
Buddy15mo 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
MrScautHDOP15mo ago
:/ What about decompile it Thats messy to right
Buddy
Buddy15mo ago
Why? No.
Accord
Accord15mo 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.
Want results from more Discord servers?
Add your server