未定义条件编译符号

发布于 2024-08-16 13:03:30 字数 1135 浏览 9 评论 0原文

我无法让 Visual Studio 按照我的预期运行。我创建了 2 个配置文件。一个定义了符号 FOO,另一个定义了符号 BAR。我有这样的代码:

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
}

然后在另一个文件中我有

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

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

另外,如果我将它放在 FOO 配置文件中,并在 #if FOO 之后输入类似 #error test 的内容,那么它将出现构建错误,但如果我删除它它会构建得很好,当我运行它时,我会得到一个编译错误,指出 MyClass 不包含 MyData 的定义。另外,这是一个 ASP.Net Web 应用程序。

有人可以帮我弄清楚如何使用条件编译吗?它的行为就好像 Visual Studio 使用编译符号正确编译它一样,但是每当 ASP.Net Web 服务器执行它时,它都会在没有任何符号的情况下重新编译它......但这没有任何意义为什么它会这样做......

编辑: 我使用 FOO 还是 BAR 配置文件并不重要,它们似乎都没有按照应有的方式定义 MyData 符号。

EDIT2:

好的,这对于复制很重要! 在解决方案中创建一个新的 App_Code 文件夹,然后在其中添加一个 .cs 文件,然后向其中添加 MyClass。这将重现在空白项目中运行的错误。我实际上已经将其简化为

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

Visual Studio 似乎没有为 App_Code 内的常规 .cs 文件设置条件编译符号

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把回忆走一遍 2024-08-23 13:03:30

由于 ASP.NET 在开发环境之外编译代码,因此它使用自己的构建配置。您必须在 web.config 中设置条件编译符号,请参阅此处的链接:
条件编译ASP.NET 2.0

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文