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

Scu*_*ode 12 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(); 
                }      
            }  
}
}  '
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏

Joh*_*aft 20

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

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

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

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述


小智 6

引用dll的.Net框架应与引用dll的项目的.Net框架版本相同