在Visual Studio (c#)的编译时设置值?

时间:2023-01-16 15:43:32

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

目前,我正在与c#合作开发一些Silverlight应用程序,使用sharepoint web服务作为数据源。

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.

sharepoint管理人员决定,为了“隔离”用户,他们必须多次创建相同的sharepoint列表,并授权每个分支的用户访问该分支的相应列表。

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.

这就产生了一个问题,因为我必须为每个分支设置sharepoint列表的guid,并编译同一个应用程序的5个不同版本。

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.

最后,我决定创建一个ListProperties类,并将当前分支的名称传递给一个方法,以便它返回正确的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.

现在,我将一个变量(branch)设置为“branch - a”,编译并将应用程序重命名为“AppBranchA.xap”。然后对每个分支做同样的操作。

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,

谢谢,

3 个解决方案

#1


2  

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

一种方法是使用条件编译符号来控制将哪个GUID值用于特定于分支的属性集。例如

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.

另一种选择是使用配置文件存储分支特定的数据。然后,您可以构建应用程序,并根据目标分支部署不同的配置文件。

#2


3  

Assuming you are using VS 2010

假设你使用的是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.

您可以使用带有分支名称的构建配置与配置转换结合使用,以更改值。然后你只需要构建每个配置。

#3


2  

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.

如果您想要变得非常花哨,您甚至可以创建新的构建(而不仅仅是调试和发布)。然后为该构建定义相应的指令。

#1


2  

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

一种方法是使用条件编译符号来控制将哪个GUID值用于特定于分支的属性集。例如

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.

另一种选择是使用配置文件存储分支特定的数据。然后,您可以构建应用程序,并根据目标分支部署不同的配置文件。

#2


3  

Assuming you are using VS 2010

假设你使用的是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.

您可以使用带有分支名称的构建配置与配置转换结合使用,以更改值。然后你只需要构建每个配置。

#3


2  

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.

如果您想要变得非常花哨,您甚至可以创建新的构建(而不仅仅是调试和发布)。然后为该构建定义相应的指令。