4

I'm not sure if this is a bug or something else.

I create a new Web Application project in VS2010. In the project, I create a new class (Class1), with the following contents:

public void Test()
{
    var s = "Hello";
    Console.WriteLine(s);
}

When I hover my mouse over s in the Console.WriteLine(s) line, a popup appears showing (local variable) string s. Great, just as I expect.

Correctly showing s to be of type string

Now, I add an App_Code folder to the project. Inside it, I again create a new class (Class2), with the exact same contents (except the class name). Now, when I hover s, it shows (local variable) var s.

Incorrectly showing s to be of type var

Why is it showing var in stead of string? Is this a bug? Can you reproduce this behavior?

It's even worse. If I move Class2.cs from the App_Code folder to the root of the project, VS2010 still doesn't show the type. Even after restarting VS2010 and reopening the project, VS2010 will show the correct type in Class1 but not in Class2. I've also tried deleting the .suo and .csproj.user files, but still no difference. Apparently VS2010 caches this information somewhere.

4

2 に答える 2

0

これはフォルダーが原因で発生していると思いApp_Codeます。App_Codeフォルダーは実行時にコンパイルされるためです。MSDNがここで説明しているように。

Web サイト プロジェクトでは、ソース コードを App_Code フォルダーに保存すると、実行時に自動的にコンパイルされます。結果のアセンブリは、Web アプリケーション内の他のコードからアクセスできます。したがって、App_Code フォルダーは Bin フォルダーと同じように機能しますが、コンパイルされたコードの代わりにソース コードを格納できる点が異なります。App_Code フォルダーと、ASP.NET Web アプリケーションでのその特別な状態により、カスタム クラスやその他のソース コードのみのファイルを作成し、それらを個別にコンパイルすることなく Web アプリケーションで使用できます。

ここにMSDNリンクがあります

于 2012-05-06T18:19:53.670 に答える
0

App_Code ディレクトリは、Web サイト プロジェクト用の特別なディレクトリです。

Web アプリケーション プロジェクトでは、App_Code ディレクトリ内に配置されたコードは VS によって無視されます。

Web サイトを Web アプリケーション プロジェクトに移行するときに、この動作に気付きました。

于 2012-05-06T18:48:03.627 に答える