visual-studio 错误“命名空间不直接包含成员,例如字段或方法”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2090369/
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
Error "A namespace does not directly contain members such as fields or methods"
提问by BoltBait
I'm trying to build my C# project and I'm getting the error message "A namespace does not directly contain members such as fields or methods". It is flagging the first character (the less than symbol) of the app.config file.
我正在尝试构建我的 C# 项目,但收到错误消息“命名空间不直接包含字段或方法等成员”。它正在标记 app.config 文件的第一个字符(小于号)。
I've checked all of my files for places where there are variables or functions directly inside of a namespace--found nothing. The app.config looks fine.
我已经检查了我的所有文件,以查找在命名空间中直接存在变量或函数的地方——一无所获。app.config 看起来不错。
Google is failing me and I'm pulling my hair out. What could be causing this error?
谷歌让我失望了,我正在拔头发。什么可能导致此错误?
回答by BoltBait
Figures! As soon as I finally break down and ask the question that I find the answer...
数据!一旦我终于崩溃并提出我找到答案的问题......
The app.config file properties section (somehow) listed the Build Action of "Compile" when it should be set to "None".
app.config 文件属性部分(以某种方式)列出了“编译”的构建操作,当它应该设置为“无”时。
How in the world did it get changed? I know I didn't change it. Grrr...
它到底是如何改变的?我知道我没有改变它。咕噜噜...
Oh well, at least it's building now. Hopefully someone else will benefit from my hairloss.
哦,好吧,至少它现在正在建设中。希望其他人能从我的脱发中受益。
回答by Guffa
I managed to reproduce the error.
我设法重现了错误。
Check the properties for the app.config file. The Build Actionshould be None, not Compile.
检查 app.config 文件的属性。本Build Action应该是None,不Compile。
回答by alin0509
I solved this way: right click on .axml file -> Build Action ->AndroidResource (xamarin studio)
我是这样解决的:右键单击 .axml 文件 -> Build Action ->AndroidResource (xamarin studio)
回答by Zwadderich
I had the same error. Checked if the app.config was set to none, no problems there. After 30 minutes of checking and rechecking, I restarted VS and that did the thing. It usually solves 90% of the unlogical errors. Sighs.
我有同样的错误。检查 app.config 是否设置为 none,没有问题。经过 30 分钟的检查和重新检查后,我重新启动了 VS 并做到了。它通常可以解决 90% 的不合逻辑的错误。叹息。
回答by Charles Driver Jr.
I was having a similar problem with the code behind in my website. The issue turned out to be an extra closing bracket that caused my class to end right after page_load(). Always check for silly syntax errors like this one.
我的网站背后的代码也有类似的问题。结果证明这个问题是一个额外的结束括号导致我的课程在 page_load() 之后立即结束。总是检查像这样的愚蠢的语法错误。
回答by Siva
I was facing this same issue with XML file. This was due to .net compiler tried to compile this xml which has been used only for DATA purpose hence no need of compilation.
我在 XML 文件中遇到了同样的问题。这是因为 .net 编译器试图编译这个仅用于 DATA 目的的 xml,因此不需要编译。
Fix : Go to property of this file and change the Build Action to content from compile.
修复:转到此文件的属性并将构建操作更改为编译内容。
Thanks
谢谢
回答by yogihosting
In my case i was by mistake putting a new function outside the controller class. This was my code in ASP.NET MVC that was giving this error.
就我而言,我错误地在控制器类之外放置了一个新函数。这是我在 ASP.NET MVC 中的代码,它给出了这个错误。
public class HomeController : Controller
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
[HttpPost]
public string Insert(string name, string age, string standard, string percent, string address, string status)
{
//some code
return "value";
}
}
It should be like:
它应该是这样的:
public class HomeController : Controller
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public string Insert(string name, string age, string standard, string percent, string address, string status)
{
//some code
return "value";
}
}
}
回答by JaredPar
This error occurs when you attempt to add a field or member directly into a namespace. Given it's pointing to app.config it's likely that VS is in a bad state. Try closing and reopening Visual Studio and see if the error goes away.
当您尝试将字段或成员直接添加到命名空间时会发生此错误。鉴于它指向 app.config,VS 可能处于不良状态。尝试关闭并重新打开 Visual Studio,看看错误是否消失。
Failing that try rebuilding and posting the results from the build output window to your question.
如果失败,请尝试重建并将结果从构建输出窗口发布到您的问题。

