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