C# .NET 属性生成“必须声明主体,因为它未标记为抽象或外部”编译错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/95683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET property generating "must declare a body because it is not marked abstract or extern" compilation error
提问by Matthew Cole
I have a .NET 3.5 (target framework) web application. I have some code that looks like this:
我有一个 .NET 3.5(目标框架)Web 应用程序。我有一些看起来像这样的代码:
public string LogPath { get; private set; }
public string ErrorMsg { get; private set; }
It's giving me this compilation error for these lines:
它给了我这些行的编译错误:
"must declare a body because it is not marked abstract or extern."
Any ideas? My understanding was that this style of property was valid as of .NET 3.0.
有任何想法吗?我的理解是,这种属性样式从 .NET 3.0 开始是有效的。
Thanks!
谢谢!
The problem turned out to be in my .sln file itself. Although I was changing the target version in my build options, in the .sln file, I found this:
问题出在我的 .sln 文件本身。尽管我在构建选项中更改了目标版本,但在 .sln 文件中,我发现了以下内容:
TargetFramework = "3.0"
Changing that to "3.5" solved it. Thanks, guys!
将其更改为“3.5”解决了它。谢谢你们!
采纳答案by R.L.
add to web.config
添加到 web.config
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4">
<providerOption name="CompilerVersion" value="v3.5" />
<providerOption name="WarnAsError" value="false" />
</compiler>
</compilers>
</system.codedom>
回答by Nikki9696
It is, as long as you put abstractin front, or implement the methods.
是的,只要你把抽象放在前面,或者实现方法。
public abstract string LogPath { get; private set; }
public abstract string ErrorMsg { get; private set; }
回答by James Curran
You are correct; that style is allowed.
你是对的; 这种风格是允许的。
I'd look into the standard assemblies referenced. I'm not sure which you'd need to get that to compile, but I figure somewhat you're pointing to the .Net v2.0 version of csc.exe.
我会研究引用的标准程序集。我不确定您需要编译哪个,但我认为您指的是 .Net v2.0 版本的 csc.exe。
回答by Darren Kopp
The syntax is valid. And you can set different access modifiers. You aren't on an Interface are you? And the class these are in isn't abstract is it?
语法有效。并且您可以设置不同的访问修饰符。你不是在接口上是吗?这些所在的类不是抽象的,是吗?
Also, doesn't matter what v. of the framework you target because this is a compiler feature. VS2008 will implement the property w/ backing stores for you.
此外,您的目标框架的哪个版本无关紧要,因为这是一个编译器功能。VS2008 将为您实现带有后备存储的属性。
回答by MADMap
Where do you define this Properties? Directly in the as*x file or in the codeBehind? (I don't think that can be a reason, but if the build-Target is .NET 3.5 I can't see anything else)
你在哪里定义这个属性?直接在 as*x 文件中还是在 codeBehind 中?(我不认为这可能是一个原因,但如果构建目标是 .NET 3.5,我看不到其他任何东西)
回答by Mike Comstock
Your code is valid - it should work fine. Go in to the property pages of your project and make sure that the "Target Framework" is .NET 3.0 or 3.5.
您的代码是有效的 - 它应该可以正常工作。进入项目的属性页并确保“目标框架”是 .NET 3.0 或 3.5。
回答by Chris Ammerman
That error should not be coming from the code you posted. According to MSDN, you've done this right: http://msdn.microsoft.com/en-us/library/bb384054.aspx
该错误不应该来自您发布的代码。根据 MSDN,你做对了:http: //msdn.microsoft.com/en-us/library/bb384054.aspx
Hence I would recommend you re-check the error message, and where the compiler says the error is coming from. The text of the message you posted did not include a reference to properties, and there is a similar message for functions... Anything that is missing an implementation and not on an interface or marked abstract or extern can generate this error.
因此,我建议您重新检查错误消息,以及编译器所说的错误来自何处。您发布的消息文本不包含对属性的引用,并且有类似的函数消息......任何缺少实现且不在接口上或标记为抽象或外部的东西都可能产生此错误。
The auto-property is a feature of the C# 3.0 language/compiler. If you are using VS 2008, it should work even if you are targeting .NET 2.0. I JUST tested it to make sure.
自动属性是 C# 3.0 语言/编译器的一项功能。如果您使用的是 VS 2008,即使您的目标是 .NET 2.0,它也应该可以工作。我只是对其进行了测试以确保。
回答by R.L.
This also happens on a raw web site project where there was no web.config generated.
这也发生在没有生成 web.config 的原始网站项目上。
Although the solution file said 3.5, .Net needed the web.config to state it also to recognize. I ran debug allowing it to create a webconfig, and all was working.
虽然解决方案文件说的是 3.5,但 .Net 需要 web.config 来声明它也能识别。我运行了调试,允许它创建一个 webconfig,一切正常。
So it is like the answer provided, but just make sure you have one.
所以它就像提供的答案一样,但只要确保你有一个。
回答by Maurice Lenz
This error can also happen if you are using CodeFile="MyControl.ascx.cs" in your MyControl.ascx instead of CodeBehind="MyControl.ascx.cs".
如果您在MyControl.ascx中使用CodeFile="MyControl.ascx.cs" 而不是CodeBehind="MyControl.ascx.cs",也会发生此错误。
In case of CodeFile, the 2.0 compiler tries to recompile the page, even if you have a WebProject instead of a WebSite and of course - does fail.
在CodeFile 的情况下,2.0 编译器会尝试重新编译页面,即使您有一个 WebProject 而不是网站,当然 - 确实会失败。
Changing the attribute name to CodeBehindfixed the problem in my case.
将属性名称更改为CodeBehind解决了我的问题。