Visual Studio 2008 doesn't recognize Lambda Ex

2019-07-24 16:18发布

问题:

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

回答1:

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).



回答2:

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.



回答3:

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.



回答4:

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 :)