Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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# App.conf自定义配置集合-无法识别的属性_C#_.net_Configuration - Fatal编程技术网

C# App.conf自定义配置集合-无法识别的属性

C# App.conf自定义配置集合-无法识别的属性,c#,.net,configuration,C#,.net,Configuration,我已经在app.config中为自定义嵌套配置集合创建了自己的类集。我现在收到一个错误,状态为“无法识别的属性‘名称’。请注意,属性名称区分大小写。” 下面是我的类、app.config的相关部分以及获取自定义配置的代码行: public class Custom : ConfigurationSection { [ConfigurationProperty("AutoSyncConfiguration")] public BackupLocationElementCollectio

我已经在app.config中为自定义嵌套配置集合创建了自己的类集。我现在收到一个错误,状态为“无法识别的属性‘名称’。请注意,属性名称区分大小写。”

下面是我的类、app.config的相关部分以及获取自定义配置的代码行:

public class Custom : ConfigurationSection
{
   [ConfigurationProperty("AutoSyncConfiguration")]
   public BackupLocationElementCollection AutoSyncConfiguration
   {
      get { return this["AutoSyncConfiguration"] as BackupLocationElementCollection; }
   }
}
public class BackupLocationElementCollection : ConfigurationElementCollection
{
   protected override ConfigurationElement CreateNewElement()
   {
      return new BackupLocationElement();
   }
   protected override object GetElementKey(ConfigurationElement element)
   {
      return ((BackupLocationElement)element).Name;
   }
   public override ConfigurationElementCollectionType CollectionType
   {
      get { return ConfigurationElementCollectionType.BasicMap; }
   }
   protected override string ElementName
   {
      get { return "BackupLocation"; }
   }
   public BackupLocationElement this[int index]
   {
      get { return (BackupLocationElement)BaseGet(index); }
      set
      {
         if (BaseGet(index) != null)
         {
            BaseRemoveAt(index);
         }
         BaseAdd(index, value);
      }
   }
   new public BackupLocationElement this[string backupName]
   {
      get { return (BackupLocationElement)BaseGet(backupName); }
   }
   public bool ContainsKey(string key)
   {
       bool result = false;
       object[] keys = BaseGetAllKeys();
       foreach (object obj in keys)
       {
         if ((string)obj == key)
         {
            result = true;
            break;
         }
      }
      return result;
   }
}

public class BackupLocationElement : ConfigurationElement
{
   [ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
   public string Name
   {
      get { return this["Name"] as string; }
      set { this["Name"] = value; }
   } 

   [ConfigurationProperty("Details", IsRequired = true, IsKey = false)]
   public string Details
   {
      get { return this["Details"] as string; }
      set { this["Details"] = value; }
   } 

   [ConfigurationProperty("WatchedFolder")]
   public WatchedFolderElementCollection WatchedFolder
   {
      get { return this["WatchedFolder"] as WatchedFolderElementCollection; }
   }
} 

public class WatchedFolderElementCollection : ConfigurationElementCollection
{
   protected override ConfigurationElement CreateNewElement()
   {
      return new WatchedFolderElement();
   }
   protected override object GetElementKey(ConfigurationElement element)
   {
      return ((WatchedFolderElement)element).Name;
   }
   public override ConfigurationElementCollectionType CollectionType
   {
      get { return ConfigurationElementCollectionType.BasicMap; }
   }
   protected override string ElementName
   {
      get { return "WatchedFolder"; }
   }
   public WatchedFolderElement this[int index]
   {
      get { return (WatchedFolderElement)BaseGet(index); }
      set
      {
         if (BaseGet(index) != null)
         {
            BaseRemoveAt(index);
         }
         BaseAdd(index, value);
      }
   }
   new public WatchedFolderElement this[string folderName]
   {
      get { return (WatchedFolderElement)BaseGet(folderName); }
   }
   public bool ContainsKey(string key)
   {
      bool result = false;
      object[] keys = BaseGetAllKeys();
      foreach (object obj in keys)
      {
         if ((string)obj == key)
         {
            result = true;
            break;
         }
      }
      return result;
   }
} 

public class WatchedFolderElement : ConfigurationElement
{
   [ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
   public string Name
   {
      get { return this["Name"] as string; }
      set { this["Name"] = value; }
   } 

   [ConfigurationProperty("LocalFolder", IsRequired = true, IsKey = false)]
   public string LocalFolder
   {
      get { return this["LocalFolder"] as string; }
      set { this["LocalFolder"] = value; }
   } 

   [ConfigurationProperty("RemoteFolder", IsRequired = true, IsKey = false)]
   public string RemoteFolder
   {
      get { return this["RemoteFolder"] as string; }
      set { this["RemoteFolder"] = value; }
   }

   [ConfigurationProperty("FileSpec", IsRequired = true, IsKey = false)]
   public string FileSpec
   {
      get { return this["FileSpec"] as string; }
      set { this["FileSpec"] = value; }
   }
}
以下是我的app.config:

<configuration>
  <configSections>
    <section name="custom" type="AutoSync.Custom, AutoSync" />
  </configSections>

  <custom>
    <AutoSyncConfiguration>
      <BackupLocation Name="S3" Details="AccessKey=asdf;SecretKey=asdf;BucketName=asdf">
        <WatchedFolder Name="test1" LocalFolder="C" RemoteFolder="Z" FileSpec="*"/>
      </BackupLocation>
      <BackupLocation Name="External" Details="MappedDrive=X;">
        <WatchedFolder Name="test" LocalFolder="D" RemoteFolder="XPhotos" FileSpec="*.jpeg" />
      </BackupLocation>
    </AutoSyncConfiguration>
  </custom>
</configuration>

有人能看到我的错误在哪里吗?

看来,按照我编写类的方式,我遗漏了层次结构的一部分。更改以下内容:

[ConfigurationProperty("WatchedFolder")]
public WatchedFolderElementCollection WatchedFolder
{
   get { return this["WatchedFolder"] as WatchedFolderElementCollection; }
}

然后修改我的app.config,使WatchedFolders元素包围WatchedFolder元素,解决了这个问题

[ConfigurationProperty("WatchedFolder")]
public WatchedFolderElementCollection WatchedFolder
{
   get { return this["WatchedFolder"] as WatchedFolderElementCollection; }
}
[ConfigurationProperty("WatchedFolders")]
public WatchedFolderElementCollection WatchedFolders
{
   get { return this["WatchedFolders"] as WatchedFolderElementCollection; }
}