未定义条件编译符号 [英] Conditional compilation symbols not being defined

查看:433
本文介绍了未定义条件编译符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得Visual Studio的行为,我会期望它。我创建了2个配置文件。一个具有定义的符号FOO,另一个具有定义的符号BAR。我有这个代码:

  static class MyClass {
#if FOO
public static string const MyData =foo defined;
#endif
#if BAR / *是的,我知道#elif会在这里工作。只是想简单* /
public static string const MyData =bar defined;
#endif
}

p>

  if(MyClass.MyData ==foo defined)..... 



好吧,在我的应用程序中,我得到一个错误,MyClass.MyData未定义。



另外,如果我在FOO配置文件,并在 #if FOO 后键入 #error test 那么它将有一个构建错误,但如果我删除它将构建正好,当我去运行它,我会得到一个编译错误,MyClass不包含MyData的定义。此外,这是一个ASP.Net Web应用程序。



有人可以帮我找出如何使用条件编译?它的行为好像Visual Studio使用编译符号正确编译它,但每当ASP.Net Web服务器执行它,它重新编译它没有任何符号...但这只是没有任何意义为什么它会这样做。 / p>

编辑:
如果我使用FOO或BAR配置文件没关系,他们似乎没有定义MyData符号。



EDIT2:



好的,这对重现来说很重要!
在您的解决方案中创建一个新的 App_Code 文件夹,然后在其中添加一个.cs文件,然后向其中添加MyClass。这将重现在空白项目中工作的错误。我实际上把它简化为只是

  #if!(FOO || BAR)
#error both foo或bar定义
#endif

看起来Visual Studio不设置条件编译符号App_Code中的常规.cs文件

解决方案

由于ASP.NET在您的开发环境之外编译代码,组态。您必须在您的web.config中设置条件编译符号,请在这里链接:
ASP.NET 2.0中的条件编译


I am having trouble getting Visual Studio to behave as I would expect it. I created 2 configuration profiles. One has the symbol FOO defined and the other has the symbol BAR defined. And I have this code:

static class MyClass{
#if FOO
  public static string const MyData="foo defined";
#endif
#if BAR /*yes, I know #elif would work here too.. just trying to be simple*/
  public static string const MyData="bar defined";
#endif
}

and then later in another file I have

 if(MyClass.MyData=="foo defined").....

Well, in my application, I get an error that MyClass.MyData is not defined.

Also, if I have it on the FOO profile and type in something like #error test after the #if FOO then it will have a build error, but if I remove that it will build just fine and when I go to run it I'll get a compilation error that MyClass does not contain a definition for MyData. Also, this is an ASP.Net Web Application.

Can someone help me figure out how to use conditional compilation? It acts as if Visual Studio compiles it properly with the compilation symbols, but whenever the ASP.Net webserver executes it, it recompiles it without any symbols... But that just doesn't make any sense why it would do that..

Edit: It doesn't matter if I use the FOO or BAR profile, neither of them seem to define the MyData symbol as they should.

EDIT2:

Ok, this is important for reproducing!!! Create a new App_Code folder in your solution and then add a .cs file there and then add MyClass to it. This will reproduce the bug working in a blank project. I've actually simplified it down to just

#if !(FOO || BAR)
  #error neither foo or bar defined
#endif

It seems like Visual Studio does not set conditional compile symbols for regular .cs files inside of App_Code

解决方案

Since ASP.NET compiles your code outside of your development environment, it uses its own build configuration. You must set the conditional compilation symbol in your web.config, see link here: Conditional Compilation in ASP.NET 2.0

这篇关于未定义条件编译符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆