Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从库中访问自定义配置部分?_C#_Configuration - Fatal编程技术网

C# 如何从库中访问自定义配置部分?

C# 如何从库中访问自定义配置部分?,c#,configuration,C#,Configuration,早些时候,我在控制台应用程序中定义了自定义部分,它工作得很好。但是现在我想把一些功能移到库中。但不幸的是,我不能。因为现在我的自定义部分被定义为null 简单控制台应用程序包括以下代码: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { NotificationsConfigurationSection sec

早些时候,我在控制台应用程序中定义了自定义部分,它工作得很好。但是现在我想把一些功能移到库中。但不幸的是,我不能。因为现在我的自定义部分被定义为null

简单控制台应用程序包括以下代码:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            NotificationsConfigurationSection section
  = NotificationsConfigurationSection.GetSection();
            string emailAddress = section.EmailAddress;
            Console.WriteLine(emailAddress);
            Console.Read();
        }
    }
}
namespace ClassLibrary1
{
    public class NotificationsConfigurationSection : ConfigurationSection
    {
        private const string configPath = "cSharpConsultant/notifications";
        public static NotificationsConfigurationSection GetSection()
        {
            return (NotificationsConfigurationSection)
              ConfigurationManager.GetSection(configPath);
        }

        /*This regular expression only accepts a valid email address. */
        [RegexStringValidator(@"[\w._%+-]+@[\w.-]+\.\w{2,4}")]
        /*Here, we register our configuration property with an attribute. 
        Note that the default value is required if we use 
        the property validation attributes, which will unfortunately 
        attempt to validate the initial blank value if a default isn't found*/
        [ConfigurationProperty("emailAddress", DefaultValue = "Address@Domain.com")]
        public string EmailAddress
        {
            get { return (string)this["emailAddress"]; }
            set { this["emailAddress"] = value; }
        }
    }
}
位于单独库中的具有自定义配置的文件包含以下代码:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            NotificationsConfigurationSection section
  = NotificationsConfigurationSection.GetSection();
            string emailAddress = section.EmailAddress;
            Console.WriteLine(emailAddress);
            Console.Read();
        }
    }
}
namespace ClassLibrary1
{
    public class NotificationsConfigurationSection : ConfigurationSection
    {
        private const string configPath = "cSharpConsultant/notifications";
        public static NotificationsConfigurationSection GetSection()
        {
            return (NotificationsConfigurationSection)
              ConfigurationManager.GetSection(configPath);
        }

        /*This regular expression only accepts a valid email address. */
        [RegexStringValidator(@"[\w._%+-]+@[\w.-]+\.\w{2,4}")]
        /*Here, we register our configuration property with an attribute. 
        Note that the default value is required if we use 
        the property validation attributes, which will unfortunately 
        attempt to validate the initial blank value if a default isn't found*/
        [ConfigurationProperty("emailAddress", DefaultValue = "Address@Domain.com")]
        public string EmailAddress
        {
            get { return (string)this["emailAddress"]; }
            set { this["emailAddress"] = value; }
        }
    }
}
在库的app.config中,我包括以下节点:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="cSharpConsultant">
      <section name="notifications"
          type="ClassLibrary1.NotificationsConfigurationSection,
        ClassLibrary1"/>
    </sectionGroup>
  </configSections>
  <cSharpConsultant>
    <notifications emailAddress="NotifiedPerson@Domain.com"/>
  </cSharpConsultant>
</configuration>


有人能帮我吗?

你是说你的app.config在库的项目中吗

配置文件必须与可执行文件位于同一项目中。自定义节的定义可以保留在库中,但配置文件本身必须与要运行的项目位于同一个项目中


在您的情况下,它应该与
ConsoleApplication1
位于同一个项目中,而不是
ClassLibrary1

实际上,您是对的。相同的app.config应该在控制台应用程序中,而不是在类库中。非常感谢。