設定用の設定ファイルを読み込む必要があるアプリケーションを作成しています。ConfigurationManagerは即座にTypeInitializationExceptionを返します
私はそうのようなApp_configファイルの設定定義しました:今、私はSSWConfig
とプットを呼び出すたび
public sealed class SSWConfig : ConfigurationSection
{
private static SSWConfig settings = ConfigurationManager.GetSection("sswcomm") as SSWConfig;
public static SSWConfig Settings
{
get { return settings; }
}
[ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
[StringValidator(InvalidCharacters = " [email protected]#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
public string Id
{
get { return (string)this["id"]; }
set { this["id"] = value; }
}
[ConfigurationProperty("connectionInterval", DefaultValue = "", IsKey = true, IsRequired = true)]
public int ConnectionInterval
{
get { return (int)this["connectionInterval"]; }
set { this["connectionInterval"] = value; }
}
[ConfigurationProperty("folders", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
public SSWConfigFolderCollection Folders
{
get { return base["folders"] as SSWConfigFolderCollection; }
}
}
public sealed class SSWConfigFolderCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new SSWConfigFolderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((SSWConfigFolderElement)element).Id;
}
new public SSWConfigFolderElement this[string Id]
{
get { return (SSWConfigFolderElement)base.BaseGet(Id); }
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
public SSWConfigFolderElement this[int index]
{
get { return (SSWConfigFolderElement)base.BaseGet(index); }
}
protected override string ElementName
{
get
{
return "folder";
}
}
}
public sealed class SSWConfigFolderElement : ConfigurationElement
{
[ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
[StringValidator(InvalidCharacters = " [email protected]#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
public string Id
{
get { return (string)base["id"]; }
set { base["id"] = value; }
}
[ConfigurationProperty("enable", DefaultValue = "true", IsKey = false, IsRequired = true)]
public bool Enable
{
get { return (string)base["enable"] == "true"; }
set { if (value) { base["enable"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("includeSubFolders", DefaultValue = "true", IsKey = false, IsRequired = true)]
public bool IncludeSubFolders
{
get { return (string)base["includeSubFolders"] == "true"; }
set { if (value) { base["includeSubFolders"] = "true"; } else { base["enable"] = "false"; } }
}
[ConfigurationProperty("fromPath", DefaultValue = "", IsKey = false, IsRequired = true)]
public string FromPath
{
get { return (string)base["fromPath"]; }
set { base["fromPath"] = value; }
}
[ConfigurationProperty("wildcard", DefaultValue = "*.*", IsKey = false, IsRequired = true)]
public string Wildcard
{
get { return (string)base["wildcard"]; }
set { base["wildcard"] = value; }
}
[ConfigurationProperty("recipientId", DefaultValue = "", IsKey = false, IsRequired = true)]
[StringValidator(InvalidCharacters = " [email protected]#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
public string RecipientId
{
get { return (string)base["recipientId"]; }
set { base["recipientId"] = value; }
}
}
:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="sswcomm" type="SSWAgent.SSWConfig" />
<section name="folders" type="SSWAgent.SSWConfigFolderCollection" />
<section name="folder" type="SSWAgent.SSWConfigFolderElement" />
</configSections>
<sswcomm id="SC_SSWTEST1" connectionInterval="5000">
<folders>
<folder id="OUTGOINGATOTEST2" enable="true" includeSubFolders="true" fromPath="C:\temp\a\NET\Outgoing_A" wildcard="*.*" recipientId="C_SSWTEST2" />
<folder id="OUTGOINGBTOTEST3" enable="true" includeSubFolders="true" fromPath="C:\temp\a\NET\Outgoing_B" wildcard="*.*" recipientId="C_SSWTEST3" />
</folders>
</sswcomm>
</configuration>
そしてそうのようなC#のコードをそれは変数で、私はすぐにTypeInitializationException
の例外を取得します。 部分がnull
を返すと思います。これはApp.configファイルで間違いを犯したためですか?
ネストされた要素を持つapp.configは私には新しいものなので、間違って定義している可能性があります。