一个新的前pression需要()或[]后类型编译错误 - C#错误、类型、pression

2023-09-07 02:54:47 作者:# 繁华落尽,谁是谁的

下面code的同事会引发以下错误,当他试图用编译它VS 2008:

The following code for a co-worker throws the following error when he tries to compile it using VS 2008:

错误:

一个新的EX pression需要()或[]   类型后

A new expression requires () or [] after type

code:

MyClass的结构:

MyClass Structure:

public class MyClass
{
    public MyClass() {}

    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

样品来源$ C ​​$ C:

Sample Source Code:

List<MyClass> x = new List<MyClass>();

x.Add(new MyClass 
{
    Property1 = "MyValue",
    Property2 = "Another Value"
});

据作品在我的机器上,而不是他。知道为什么吗?

It "works on my machine", but not his. Any idea why?

更新 他的目标是在3.5 .NET框架 他使​​用的是System.Collections.Generics命名空间 在MyClass的对象确实有一个构造

UPDATE He is targeting the 3.5 .NET framework He is using the System.Collections.Generics namespace The MyClass object does have a constructor

更新1: @ Funky81 - 你的榜样,我的例子能编译我的电脑

UPDATE 1: @Funky81 - Your example and my example were able to compile on my PC.

更新2: MyClass的的样本包括架构

Update 2: Included schema of MyClass in sample

更新3: @DK - 我有我的同事以下配置部分添加到自己的应用程序:

UPDATE 3: @DK - I had my co-worker add the following configuration section to his application:

<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=b77a5c561934e089">
    			<providerOption name="CompilerVersion" value="v3.5"/>
    			<providerOption name="WarnAsError" value="false"/>
    		</compiler>
    		<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    			<providerOption name="CompilerVersion" value="v3.5"/>
    			<providerOption name="OptionInfer" value="true"/>
    			<providerOption name="WarnAsError" value="false"/>
    		</compiler>
    	</compilers>
    </system.codedom>

和他收到以下编译错误:无法识别的元素providerOption

And he received the following compilation error: Unrecognized element 'providerOption'.

推荐答案

下面是什么,似乎是唯一的相似,但不完全相同的VS.2008可用同样的,错误:

Here's what seems to be the only similar, but not exactly the same, error available in VS.2008:

编译器错误CS1526:新   EX pression需要(),[] 或{}   类型后

Compiler Error CS1526 : A new expression requires (), [], or {} after type

请注意这些 {} 的错误信息,这是C#3.0语法的一部分。这是不相关的框架版本,但是到了语言的版本。

Note those {} in error message, which are part of c# 3.0 syntax. This is not related to framework version, but to the version of the language.

我敢打赌,用某种方式不同版本的编译器。

My bet is that somehow a different version of compiler was used.

补充:这看起来像与ASP.Net一个可能的问题。地方,检查是在config文件(S),元素

Added: this looks like a likely issue with ASP.Net. Place to check is in .config file(s), element

configuration\system.codedom\compilers\compiler @language="c#..."

应该有

<providerOption name="CompilerVersion" value="v3.5"/>
 
精彩推荐
图片推荐