我得到了“缺少使用指令或汇编参考”,并且不知道出了什么问题

时间:2013-06-27 13:18:38

标签: c# asp.net asp.net-mvc visual-studio-2010

我正在尝试允许用户将数据输入到将添加到web.config文件的文本框中。我已经将相关的行添加到了web.config文件中,但是当我使这个类出错时都会出错。

每当我尝试运行我的应用程序时,我一直在想你是否错过了using指令或程序集refenrence错误。我已经看过其他时候这个问题已被问到,似乎无法弄清楚我哪里出错了。问题是我对Visual Studio非常陌生,我只是留下了空白的答案。

下面是生成错误的类文件。我希望我已经包含了你需要的一切来帮助我。谢谢。

 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(); 
                }      
            }  
}
}  '

感谢任何帮助

8 个答案:

答案 0 :(得分:19)

您可能没有将System.Configuration dll添加到项目引用中。它默认不存在,您必须手动添加它。

右键单击References并在.net程序集中搜索System.Configuration。

检查它是否在您的参考文献中......

enter image description here

右键单击并选择添加引用...

enter image description here

在.Net程序集列表中查找System.Configuration,选择它,然后单击Ok ...

enter image description here

程序集现在应该出现在您的参考文献中......

enter image description here

答案 1 :(得分:4)

引用dll的.Net框架应该与引用dll的Project的.Net框架版本相同

答案 2 :(得分:0)

using语句显示正确。

您是否错过了System.configuration.dll的汇编引用?

右键单击项目中的“References”文件夹,然后单击“Add Reference ...”

答案 3 :(得分:0)

此问题可能是由于您的应用程序缺少对您尝试使用代码的外部DLL的引用。通常,Visual Studio应该让您了解哪些对象不知道该怎么做,这应该是朝着正确方向迈出的一步。

您需要查看解决方案资源管理器并右键单击项目引用,然后转到添加 - &gt;并查找你需要的那个。它很可能是大多数人在这里指出的System.Configuration程序集,而应该在引用窗口的Framework选项下。这应该可以解决您的问题。

答案 4 :(得分:0)

我在第一行和最后一行的末尾都发现了一个引用'

'using System.Collections.Generic;

您的原始代码或格式错误是否存在?

答案 5 :(得分:0)

我今天早些时候遇到了同样的问题。我无法弄清楚为什么我试图引用的类文件没有被编译器看到。我最近将有问题的类文件的命名空间更改为另一个但已经存在的命名空间。 (我还使用了对类的新的和以前的命名空间的引用,我试图将其实例化)

当编译器告诉我在尝试实例化类时缺少引用时,我右键单击并点击“生成类存根”。一旦Visual Studio为我生成了类存根,我将旧类文件中的代码应用并粘贴到此存根中,保存存根,当我再次尝试编译时,它工作正常!没问题。

可能是我的构建特有的解决方案,但值得一试。

答案 6 :(得分:0)

以下技术对我有效:

1)右键单击项目解决方案->单击清洁解决方案

2)右键单击项目解决方案->单击“重建解决方案”

答案 7 :(得分:0)

在某些情况下,当显然需要添加using并且Studio无法看到此名称空间时,Studio重新启动可以节省一天的时间。