Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting values at compile-time in Visual Studio (C#)?

I'm currently working with C# and developing a few Silverlight applications that use sharepoint web services as data sources.

The sharepoint admins decided that in order to "segregate" users, they had to create the same sharepoint list multiple times, and grant users from each branch access to the corresponding list for that branch.

That creates a bit of a problem because I have to set the guid for the sharepoint lists for each branch, and compile 5 different version of the same application.

Eventually I decided to create a ListProperties class, and pass the name of the current branch to a method so that it returns the right guid.

So now, I set a variable (branch) to "BRANCH-A", compile and rename the app to "AppBranchA.xap". Then do the same for each branch.

Is there any way to pass a string at compile-time and have the variable (and hopely the app name too) changed automatically?

I'm willing to hear out other ways to manage multiple builds at once.

thanks,

like image 367
robertrv Avatar asked Nov 24 '10 21:11

robertrv


3 Answers

Assuming you are using VS 2010

You can use Build configurations with the branch name in conjunction with config transforms to change the value. Then you just have to build in each configuration.

like image 184
Josh Avatar answered Nov 15 '22 04:11

Josh


One way to approach this is to use a conditional compilation symbol to control which GUID value is used for set of branch specific properties. For example

class ListProperties {

#if BRANCH-A
  public static readonly GUID BranchGuid = "Guid #1";
#endif

#if BRANCH-B
  public static readonly GUID BranchGuid = "Guid #2";
#endif

}

This allows you to control the value of the branch definitions by changing the defined pre-processor symbols on the command line.

Another option would be to use a config file to store the branch specific data. You could then build the application once and deploy it with different config files based on the target branch.

like image 41
JaredPar Avatar answered Nov 15 '22 06:11

JaredPar


If you use precompile directives, you could achieve this.

#if BRANCH_A
 readonly Guid myId = new Guid("some guid");
#endif

#if BRANCH_B
 readonly Guid myId = new Guid("some other guid");
#endif

If you wanted to get really fancy, you could even create new builds (rather than just debug and release). Then you have your corresponding directives defined for that build.

like image 41
scottm Avatar answered Nov 15 '22 04:11

scottm