.NET - Error trying to compile Automatic Properties
-
21-08-2019 - |
Question
I'm trying to compile a POCO with this code
public class MenuItem
{
public string Name
{ get; set; }
public string Url
{ get; set; }
}
I keep getting compile errors on the gets and sets with messages like: 'MenuItem.Name.get' must declare a body because it is not marked abstract or extern. What am I missing? I'm compiling this class in the App_Code folder of a local filesystem web site that is set to compile as .NET 3.5. I know I have done this before, but can't figure out what I am doing differently.
Solution
Make sure your Web.config
file contains a <system.codedom>
subelement under <configuration>
element like this:
<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>
</compilers>
</system.codedom>
The problem arises from the fact that ASP.NET is running the old version of C# compiler to compile your application (v2.0) which does not support automatic properties. In order to use .NET 3.5 features, you have to specify the compiler version in your Web.config
explicitly.