C
C#11mo ago
MrScautHD

❔ How can i remove methods or classes with an Attribute from compiling?

How can i remove methods or classes with an Attribute from compiling?
30 Replies
MrScautHD
MrScautHD11mo ago
ok what i need todo?
Angius
Angius11mo ago
That seems like an XY issue tbh
MrScautHD
MrScautHD11mo ago
would it not possible with a attribute
Angius
Angius11mo ago
Ah, so trying to fix a fucked up architecture, carry on
MrScautHD
MrScautHD11mo ago
is the #if the best way? would it possible to set the #if variable with c# code or just with XNA :/ what is #define doing? eh i not think they can i just like the code style more instead of the #if
Angius
Angius11mo ago
Attributes are something you get on runtime during reflection, or on compile time using source generators You can't arbitrarily exclude code from compilation using them Since reflections cannot do that And source generators can only generate code, not delete or overwrite it So either: a) rethink your architecture b) deal with it
MrScautHD
MrScautHD11mo ago
man i just find the best way but yea thx i use the #if thing if it would work with my system @Mathf.Lerp i doing a API so i need to allow other users to disable or enable it and i think you can not overwrite it
Angius
Angius11mo ago
Uh, an API is compiled once, and runs on the server Unless you want the users to be able to recompile it? And remove arbitrary code? Which seems, uh, mental
MrScautHD
MrScautHD11mo ago
idk would it not possible to doing any generated file that allow you to set the headless variable to true or false
Angius
Angius11mo ago
With #if you can check for variables in the csproj
MrScautHD
MrScautHD11mo ago
yea ik so i need to change it before i compile it
Angius
Angius11mo ago
Or just use simple logic where needed
var user = some_env_var_or_something_idk
? new FooUser()
: new BarUser();
var user = some_env_var_or_something_idk
? new FooUser()
: new BarUser();
MrScautHD
MrScautHD11mo ago
oh wait
MrScautHD
MrScautHD11mo ago
if i doing now something like that?
MrScautHD
MrScautHD11mo ago
can i define it in a project that refernce this?
Angius
Angius11mo ago
https://stackoverflow.com/a/61122781/6042255 ye Well, uh, kinda Weird, I thought you can just easily use csproj variables, but it seems like you need #define after all
MrScautHD
MrScautHD11mo ago
if i define that now:
MrScautHD
MrScautHD11mo ago
should it not normaly not anymore possbile to call the Input?
MrScautHD
MrScautHD11mo ago
Because it still is
MrScautHD
MrScautHD11mo ago
mhh i see that would not work
JakenVeina
JakenVeina11mo ago
the closes thing to what you're asking is [Conditional()] which only works because Roslyn is designed to specifically look for it, and change its behavior accordingly
JakenVeina
JakenVeina11mo ago
ConditionalAttribute Class (System.Diagnostics)
Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.
JakenVeina
JakenVeina11mo ago
other than that, yes, this very much sounds like an XY problem
MrScautHD
MrScautHD11mo ago
Does that really remove it from the compiler or just ingore it?
JakenVeina
JakenVeina11mo ago
not entirely sure
MrScautHD
MrScautHD11mo ago
mhh kk but if i want to remove it from the compiler i need to use #if
JakenVeina
JakenVeina11mo ago
in some form, yes
MrScautHD
MrScautHD11mo ago
and you guys know no other ways that i can remove it with a attribute
JakenVeina
JakenVeina11mo ago
no there is no such thing attributes do nothing ever if you want things to be excluded from your build, you need to talk to the thing that builds it: the compiler the compiler's support for excluding things from builds includes that attribute I linked to, and pre-processor directives
Accord
Accord11mo 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.