0

I'm using VS2010 professional, and I have a project, but i want to have a way that compiles only some of the code in one configuration, and the rest in another. #ifdef is rather clunky, as it must surround said code, and there are lots of fragments. I was thinking like an attribute. And if theres any minecraft modders here, i basically need forge's @SideOnly for C#.

public class Foo{
    [BuildConfig("Config1")]
    public void Bar(){}
    [BuildConfig("Config2")]
    public void Baz(){}
}

compiling this code under Config1 would leave out Baz, and vice versa.

AlphaModder
  • 3,266
  • 2
  • 28
  • 44
  • 1
    Why is your program dependent on only being partially compiled, particularly in a way so complex that an #if won't work? It would make more sense to have the parts you want compiled in one context separate from the parts you want compiled in a different context. – Magus Jan 30 '14 at 16:27
  • It's a Client-Server thing. I want the client to have only client code and the server to only have server code, but it seems wasteful to rewrite common code. – AlphaModder Jan 30 '14 at 16:30
  • i.e. rendering is client-only, and logic is server-only – AlphaModder Jan 30 '14 at 16:31
  • Why are rendering and logic in the same class? Why are client and server classes in the same assembly? – Magus Jan 30 '14 at 16:40
  • Say i have an Item class... and a server needs to perform the function of it, and a client needs to render it in an inventory. – AlphaModder Jan 30 '14 at 16:51
  • Sounds like you should be passing a data object, and consuming it with separate implementations. I fail to see why a data object needs to know about rendering or said function. Don't combine unrelated concerns. If you don't need something in one place, don't put it there in the first place. – Magus Jan 30 '14 at 16:57

1 Answers1

0

Comments about why intermixing unrelated code is not good aside, you could use the ConditionalAttribute, but only on void methods. This compiles out the code at the callsite as well. For other areas of your code, you'd have to resort back to #ifdef.

Kit
  • 20,354
  • 4
  • 60
  • 103