C# 我收到“缺少 using 指令或程序集引用”的消息,并且不知道出了什么问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17344295/
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
I'm getting the "missing a using directive or assembly reference" and no clue what's going wrong
提问by Scubacode
I'm trying to allow a user to enter data into a textbox that will be added to the web.config file. I've added the relevent lines to the web.config file but when I make this class all goes wrong.
我试图允许用户将数据输入到将添加到 web.config 文件的文本框中。我已经将相关行添加到 web.config 文件中,但是当我创建这个类时,一切都出错了。
I keep getting the are you missing a using directive or assembly refenrence error whenever I try to run my app. I have looked at the other times this question has been asked and can't seem to figure out where I'm going wrong. The thing is that I am extremely new to Visual Studio and am just left blank at what could be the answer.
每当我尝试运行我的应用程序时,我不断收到您是否缺少 using 指令或程序集引用错误。我已经看过其他时候问过这个问题,但似乎无法弄清楚我哪里出错了。问题是我对 Visual Studio 非常陌生,对可能的答案一无所知。
Below here is the class file that's generating the error. I hope I've included everything you need to assist me. Thank you.
下面是生成错误的类文件。我希望我已经包括了你需要帮助我的一切。谢谢你。
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
namespace WebConfigDemo
{
public class CompanyConfigSection : ConfigurationSection
{
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public CompanyConfigCollection Companies
{
get
{
return (CompanyConfigCollection)this[""];
}
set
{
this[""] = value;
}
}
}
public class CompanyConfigElement : ConfigurationElement
{
[ConfigurationProperty("id", IsKey = true, IsRequired = true)]
public int Id
{
get
{
return (int)this["id"];
}
set
{
this["id"] = value;
}
}
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return this["name"].ToString();
}
set
{
this["name"] = value;
}
}
} '
public class CompanyConfigCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new CompanyConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CompanyConfigElement)element).Id;
}
}
public class CompaniesConfig
{
private static readonly Dictionary<int, CompanyConfigElement>
Elements;
static CompaniesConfig()
{
Elements = new Dictionary<int, CompanyConfigElement>();
var section = (CompanyConfigSection)ConfigurationManager.GetSection ("companies");
foreach (CompanyConfigElement system in section.Companies)
Elements.Add(system.Id, system);
}
public static CompanyConfigElement GetCompany(int companyId)
{
return Elements[companyId];
}
public static List<CompanyConfigElement> Companies
{
get
{
return Elements.Values.ToList();
}
}
}
} '
Any help is appreciated
任何帮助表示赞赏
采纳答案by John Kraft
You probably don't have the System.Configuration dll added to the project references. It is not there by default, and you have to add it manually.
您可能没有将 System.Configuration dll 添加到项目引用中。默认情况下它不存在,您必须手动添加它。
Right-click on the References and search for System.Configuration in the .net assemblies.
右键单击引用并在 .net 程序集中搜索 System.Configuration。
Check to see if it is in your references...
检查它是否在您的参考文献中...


Right-click and select Add Reference...
右键单击并选择添加引用...


Find System.Configuration in the list of .Net Assemblies, select it, and click Ok...
在 .Net Assemblies 列表中找到 System.Configuration,选中它,然后单击 Ok...


The assembly should now appear in your references...
该程序集现在应该出现在您的参考文献中...


回答by Samuel Parkinson
Your usingstatements appear to be correct.
你的using陈述似乎是正确的。
Are you, perhaps, missing the assembly reference to System.configuration.dll?
您是否可能缺少对 的程序集引用System.configuration.dll?
Right click the "References" folder in your project and click on "Add Reference..."
右键单击项目中的“引用”文件夹,然后单击“添加引用...”
回答by Jesse Carter
This problem would be caused by your application missing a reference to an external dll that you are trying to use code from. Usually Visual Studio should give you an idea about which objects that it doesn't know what to do with so that should be a step in the right direction.
此问题可能是由于您的应用程序缺少对您尝试使用其代码的外部 dll 的引用造成的。通常,Visual Studio 应该让您了解它不知道如何处理哪些对象,因此这应该是朝着正确方向迈出的一步。
You need to look in the solution explorer and right click on project references and then go to add -> and look up the one you need. It's most likely the System.Configuration assembly as most people have pointed out here while should be under the Framework option in the references window. That should resolve your issue.
您需要查看解决方案资源管理器并右键单击项目引用,然后转到添加 -> 并查找您需要的那个。正如大多数人在这里指出的那样,它很可能是 System.Configuration 程序集,而应该在参考窗口中的 Framework 选项下。那应该可以解决您的问题。
回答by Microsoft DN
I have observed a quote 'in your 1st line and also at the end of your last line.
我'在你的第一行和最后一行的末尾观察到了一个引用。
'using System.Collections.Generic;
Is this present in your original code or some formatting mistake?
这是否存在于您的原始代码中或某些格式错误?
回答by Alex Vallejo
I had the same problem earlier today. I could not figure out why the class file I was trying to reference was not being seen by the compiler. I had recently changed the namespace of the class file in question to a different but already existing namespace. (I also had using references to the class's new and previous namespaces where I was trying to instantiate it)
我今天早些时候遇到了同样的问题。我无法弄清楚为什么编译器没有看到我试图引用的类文件。我最近将相关类文件的命名空间更改为不同但已经存在的命名空间。(我还使用了对类的新的和以前的命名空间的引用,我试图在其中实例化它)
Where the compiler was telling me I was missing a reference when trying to instantiate the class, I right clicked and hit "generate class stub". Once Visual Studio generated a class stub for me, I coped and pasted the code from the old class file into this stub, saved the stub and when I tried to compile again it worked! No issues.
编译器告诉我在尝试实例化类时缺少引用的地方,我右键单击并点击“生成类存根”。一旦 Visual Studio 为我生成了一个类存根,我就处理并将旧类文件中的代码粘贴到这个存根中,保存了存根,当我再次尝试编译时它起作用了!没有问题。
Might be a solution specific to my build, but its worth a try.
可能是特定于我的构建的解决方案,但值得一试。
回答by Gayatri Dingare
.Net framework of the referencing dll should be same as the .Net framework version of the Project in which dll is referred
引用 dll 的 .Net 框架应与引用 dll 的项目的 .Net 框架版本相同
回答by Akash V
The following technique worked for me:
以下技术对我有用:
1) Right click on the project Solution -> Click on Clean solution
1) 右键单击项目解决方案 -> 点击清理解决方案
2) Right click on the project Solution -> Click on Rebuild solution
2)右键单击项目解决方案->单击重建解决方案

