C
C#16mo 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
MrScautHDOP16mo ago
ok what i need todo?
Angius
Angius16mo ago
That seems like an XY issue tbh
MrScautHD
MrScautHDOP16mo ago
would it not possible with a attribute
Angius
Angius16mo ago
Ah, so trying to fix a fucked up architecture, carry on
MrScautHD
MrScautHDOP16mo 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
Angius16mo 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
MrScautHDOP16mo 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
Angius16mo 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
MrScautHDOP16mo ago
idk would it not possible to doing any generated file that allow you to set the headless variable to true or false
Angius
Angius16mo ago
With #if you can check for variables in the csproj
MrScautHD
MrScautHDOP16mo ago
yea ik so i need to change it before i compile it
Angius
Angius16mo 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
MrScautHDOP16mo ago
oh wait
MrScautHD
MrScautHDOP16mo ago
if i doing now something like that?
MrScautHD
MrScautHDOP16mo ago
can i define it in a project that refernce this?
Angius
Angius16mo 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
MrScautHDOP16mo ago
if i define that now:
MrScautHD
MrScautHDOP16mo ago
should it not normaly not anymore possbile to call the Input?
MrScautHD
MrScautHDOP16mo ago
Because it still is
MrScautHD
MrScautHDOP16mo ago
mhh i see that would not work
JakenVeina
JakenVeina16mo 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
JakenVeina16mo 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
JakenVeina16mo ago
other than that, yes, this very much sounds like an XY problem
MrScautHD
MrScautHDOP16mo ago
Does that really remove it from the compiler or just ingore it?
JakenVeina
JakenVeina16mo ago
not entirely sure
MrScautHD
MrScautHDOP16mo ago
mhh kk but if i want to remove it from the compiler i need to use #if
JakenVeina
JakenVeina16mo ago
in some form, yes
MrScautHD
MrScautHDOP16mo ago
and you guys know no other ways that i can remove it with a attribute
JakenVeina
JakenVeina16mo 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
Accord16mo 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