Visual Studio 2008 无法识别 Lambda 表达式语法

发布于 2024-07-10 05:45:15 字数 851 浏览 4 评论 0原文

我最近使用内置转换工具将 Web 应用程序项目(以及一些相关项目)从 .net 2.0 升级到 .net 3.5。 一切都运行良好,例如使用 MS AJAX 3.5 与 2.0 中的外部 MS AJAX 库。

当我尝试使用新的 Lambda 表达式语法时,出现了问题。 编译器不会将 Lambda 表达式识别为有效语法。 解决方案中的所有项目中的目标框架版本均设置为 3.5。我还能够在同一解决方案的库项目中成功使用 Lambda 表达式。

这是给我错误的代码。 没什么特别的。

ObjectFactory.Initialize(x => 
        {
            x.ForRequestedType<IUnitIdSequencingService>().TheDefaultIsConcreteType<UnitIdSequencingService>();
            x.ForRequestedType<IGadgetDAO>().TheDefault.Is.OfConcreteType<GadgetDAO>().WithCtorArg("instance").EqualToAppSetting("OSHAInspectionManager");

        });

我收到的具体错误是:

Error   102 Invalid expression term '>' D:\projects\bohlco\pmr\PMR\Web\App_Code\Bootstrapper.cs 13  41  D:\...\Web\

任何帮助将不胜感激。 我一直在谷歌上搜索,但运气不佳

I recently upgraded a Web Application Project (as well as some dependent projects) from .net 2.0 to .net 3.5 using the built in conversion tool. Everything works well such as using MS AJAX 3.5 vs. the external MS AJAX libraries in 2.0.

My problem occurs when I tried using the new Lambda Expression syntax. The compiler will not recognize Lambda Expressions as valid syntax. The target frame work version is set to 3.5 in all projects in the solution.I was also able to successfully use Lambda Expressions in a Library Project in the same solution.

The is the code that is giving me the error. Nothing too special.

ObjectFactory.Initialize(x => 
        {
            x.ForRequestedType<IUnitIdSequencingService>().TheDefaultIsConcreteType<UnitIdSequencingService>();
            x.ForRequestedType<IGadgetDAO>().TheDefault.Is.OfConcreteType<GadgetDAO>().WithCtorArg("instance").EqualToAppSetting("OSHAInspectionManager");

        });

The specific errors I am getting are:

Error   102 Invalid expression term '>' D:\projects\bohlco\pmr\PMR\Web\App_Code\Bootstrapper.cs 13  41  D:\...\Web\

Any help would be greatly appreciated. I have been searching Google with little luck

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

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

发布评论

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

评论(4

本王不退位尔等都是臣 2024-07-17 05:45:15

如果任何页面正在由 ASP.NET 编译(即您没有预编译 WAP),那么您需要确保 ASP.NET 了解 C# 3.0 (.NET 3.5) 编译器。 确保 web.config 中包含以下内容:

<system.codedom>
   <compilers>
      <compiler language="c#;cs;csharp"
            extension=".cs"
            warningLevel="4"
            type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=
         <providerOption name="CompilerVersion" value="v3.5"/>
         <providerOption name="WarnAsError" value="false"/>
      </compiler>
   </compliers>
</system.codedom>

此外,如果您在 IIS 中托管,请确保将正确的文件夹设置为应用程序,并且它使用的是 ASP.NET v2.blah(不是v1.1.blah)。

If any of the page is being compiled by ASP.NET (i.e. you aren't pre-compiling the WAP), then you'll need to ensure that ASP.NET knows about the C# 3.0 (.NET 3.5) compiler. Ensure the following is in the web.config:

<system.codedom>
   <compilers>
      <compiler language="c#;cs;csharp"
            extension=".cs"
            warningLevel="4"
            type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=
         <providerOption name="CompilerVersion" value="v3.5"/>
         <providerOption name="WarnAsError" value="false"/>
      </compiler>
   </compliers>
</system.codedom>

Also, if you are hosting in IIS, ensure that the correct folder is set as an application, and that it is using ASP.NET v2.blah (not v1.1.blah).

眉目亦如画i 2024-07-17 05:45:15

我对 VS 2008 转换工具没有太多经验,但我知道其他项目转换工具也有“问题”。 我建议您将“损坏”项目的 .csproj 文件与正在运行的项目进行比较。 也许转换实用程序破坏了您项目中的某些内容。 您还可以尝试创建一个新项目并复制所有源文件。

I don't have much experience with the VS 2008 conversion tool, but I know other project conversion tools have had "issues". I'd recommend you compare the .csproj file for your 'broken' project to one that is working. Maybe the conversion utility broke something in your project. You could also try creating a new project and copying over all the source files as well.

却一份温柔 2024-07-17 05:45:15

您需要跳过现有项目重新引用的几个环节,说实话,我发现创建一个新项目并将现有源文件添加到新项目中更容易。

couple of hoops you need to jump through with existing projects re references, TBH I found it easier to just create a new project and add my existing source files to the new project.

客…行舟 2024-07-17 05:45:15

我猜测您将 lambda 传递到的方法的参数接受委托作为参数?

如果这是真的,那么您需要将 lambda 转换为特定类型的委托。 这有点令人困惑,但您需要知道的是 lambda 并不总是能够正确推断,因此您需要对其进行强制转换或更改方法签名以接受特定类型的委托。

试试这个:

ObjectFactory.Initialize((Action<T>)(x => // where T is the typeof x
{
    // ...
}));

您也可以尝试为 Initialize 进行一些重载以接受特定类型的委托(例如 Action)。

如果您的方法确实接受特定类型的委托类型,那么您可以忽略此答案:)

I'm guessing the parameter to the method you are passing the lambda into accepts a Delegate as a parameter?

If this is true then you will need to cast the lambda as a specific type of delegate. This is sort of confusing but what you need to know is that a lambda can't always be inferred correctly so you need to cast it or change the method signature to accepts specific types of delegates.

try this:

ObjectFactory.Initialize((Action<T>)(x => // where T is the typeof x
{
    // ...
}));

Also you could try making a few overloads for Initialize to accept specific types of delegates (such as Action).

If your method does accept a specific type of delegate type than you can ignore this answer :)

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