代码需要使用foreach对我自己的自定义appSettings

代码需要使用foreach对我自己的自定义appSettings

本文介绍了代码需要使用foreach对我自己的自定义appSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索过该网站,但没有找到我要找的内容。



基本上我想有一个这样的配置部分:

 < configSections> 
< section name =PhoneNotificationsSectiontype =Alerts.PhoneAlertConfigSection,Alerts,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null/>
< / configSections>
< PhoneNotificationsSection>
< phones>
< add phone =MyMobilevalue [email protected]/>
< add phone =OtherMobilevalue [email protected]/>
< / phones>
< / PhoneNotificationsSection>

然后,我想在我的appSettings消费代码中,伪代码):

  foreach(phone中的phone p)
{
//'phone'只是有帮助/描述
DoSomething(p.value);
}



我已经做了足够的研究,以了解我可能需要一些自己的实现和/或继承某些配置类以使上述代码成为可能的类。我只是没有找到任何清楚地表明这种情况和如何编码它 - 当我试图学习整个.NET配置世界我的大脑开始受伤。任何人都有一些像我正在寻找的代码,他们可以共享?

解决方案

一个C#课程的例子。在我看来,它主要演示了.NET配置子系统的可怕性,尽管代码可以工作。我没有适应你的设置,因为它很容易引入一个错误,到目前为止,SO编辑器不验证发布的代码示例;)



首先,配置节声明:

 < configSections> 
< section name =passwordSafe
type =Codeworks.PasswordSafe.Model.Configuration.PasswordSafeSection,Codeworks.PasswordSafe.Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null >
< / configSections>

< passwordSafe hashAlgorithm =SHA256>
< users>
< user name =mmpassword =Jok2eyBcFs4y7UIAlCuLix4mLfxw2byfvHfElpmk8d8 =/>
< user name =joepassword =Jok2eyBcFs4y7UIAlCuLix4mLfxw2byfvHfElpmk8d8 =/>
< / users>
< / passwordSafe>为了匹配上面的代码片段,我们首先需要配置部分:






  public class PasswordSafeSection:ConfigurationSection 
{
#region静态访问器
///< summary>
///使用默认元素名称获取配置节。
///< / summary>
public static PasswordSafeSection GetSection()
{
return GetSection(passwordSafe);
}

///< summary>
///使用指定的元素名称获取配置节。
///< / summary>
public static PasswordSafeSection GetSection(string sectionName)
{
PasswordSafeSection section = ConfigurationManager.GetSection(sectionName)as PasswordSafeSection;
if(section == null)
{
string message = string.Format(找不到指定的配置节(< {0}>)。
throw new ConfigurationErrorsException(message);
}
return section;
}
#endregion

#region配置属性
[ConfigurationProperty(hashAlgorithm)]
public string HashAlgorithm
{
get {return(string)this [hashAlgorithm]; }
set {this [hashAlgorithm] = value; }


[ConfigurationProperty(users,IsDefaultCollection = true)]
public UserElementCollection用户
{
get {return(UserElementCollection)this [ users]; }
set {this [users] = value; }
}

public override bool IsReadOnly()
{
return false;
}
#endregion
}
  [ConfigurationCollection(typeof(UserElement),CollectionType = ConfigurationElementCollectionType.BasicMap)] 
public class UserElementCollection:ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new UserElement();
}

protected override string ElementName
{
get {returnuser; }
}
public override ConfigurationElementCollectionType CollectionType
{
get {return ConfigurationElementCollectionType.BasicMap; }
}

public override bool IsReadOnly()
{
return false;
}

#region索引器
public UserElement this [int index]
{
get {return BaseGet(index)as UserElement; }
set
{
if(BaseGet(index)!= null)
{
BaseRemoveAt(index);
}
BaseAdd(index,value);
}
}

public new UserElement this [string name]
{
get {return BaseGet(name)as UserElement; }
}
#endregion

#region查找方法
protected覆盖对象GetElementKey(ConfigurationElement元素)
{
UserElement user = element as UserElement;
return user!= null? user.UserName:error;
}

public string GetKey(int index)
{
return(string)BaseGetKey(index);
}
#endregion

#region添加/删除/清除方法
public void Add(UserElement item)
{
BaseAdd );
}

public void Remove(string name)
{
BaseRemove(name);
}

public void Remove(UserElement item)
{
BaseRemove(GetElementKey(item));
}

public void RemoveAt(int index)
{
BaseRemoveAt(index);
}

public void Clear()
{
BaseClear();
}
#endregion
}

声明元素集合中使用的自定义元素:

  public class UserElement:ConfigurationElement 
{
#region构造函数
public UserElement()
{
}

public UserElement(string userName,string passwordHash)
{
UserName = userName;
PasswordHash = passwordHash;
}
#endregion

#region配置属性
[ConfigurationProperty(name,IsKey = true)]
public string UserName
{
get {return(string)this [name]; }
set {this [name] = value; }
}

[ConfigurationProperty(password,IsRequired = true)]
public string PasswordHash
{
get {return(string)this [ password]; }
set {this [password] = value; }
}

public override bool IsReadOnly()
{
return false;
}
#endregion
}

我们就可以访问配置文件了。我使用一个Configurator助手类使这稍微不太繁琐:

  public static class Configurator 
{
#region AppSettings Helpers
public static int SplashScreenDisplayTime
{
get {return Convert.ToInt32(ConfigurationManager.AppSettings [splash.display.msecs]); }
}
#endregion

#region用户助手
public static bool TryGetUserPasswordHash(string userName,out string passwordHash)
{
UserElement user = GetUser(userName);
passwordHash = user!= null? user.PasswordHash:null;
return! string.IsNullOrEmpty(passwordHash);
}

private static UserElement GetUser(string userName)
{
SystemConfiguration config = GetConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
PasswordSafeSection section = config.Sections [passwordSafe] as PasswordSafeSection;
return section.Users [userName];
}
public static void AddUser(string userName,string passwordHash,string encryptionKey)
{
SystemConfiguration config = GetConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
PasswordSafeSection section = config.Sections [passwordSafe] as PasswordSafeSection;
UserElement user = section.Users [userName];
if(user == null)
{
user = new UserElement(userName,passwordHash,encryptionKey);
section.Users.Add(user);
config.Save(ConfigurationSaveMode.Modified);
}
}
public static void RemoveUser(string userName)
{
SystemConfiguration config = GetConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
PasswordSafeSection section = config.Sections [passwordSafe] as PasswordSafeSection;
section.Users.Remove(userName);
config.Save(ConfigurationSaveMode.Modified);
}
public static void UpdateUser(string userName,string passwordHash)
{
SystemConfiguration config = GetConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
PasswordSafeSection section = config.Sections [passwordSafe] as PasswordSafeSection;
UserElement user = section.Users [userName];
if(user!= null)
{
user.PasswordHash = passwordHash;
config.Save(ConfigurationSaveMode.Modified);
}
}
#endregion

#region配置助手
private static SystemConfiguration GetConfiguration(ConfigurationUserLevel userLevel)
{
SystemConfiguration config = InitializeConfiguration(userLevel);
return config;
}

private static SystemConfiguration InitializeConfiguration(ConfigurationUserLevel userLevel)
{
SystemConfiguration config = ConfigurationManager.OpenExeConfiguration(userLevel);
PasswordSafeSection section = config.Sections [passwordSafe] as PasswordSafeSection;
if(section == null)
{
section = new PasswordSafeSection();
section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
section.SectionInformation.ForceSave = true;
config.Sections.Add(passwordSafe,section);
config.Save(ConfigurationSaveMode.Full);
}
return config;
}
#endregion
}

希望这有帮助。 / p>

I've searched the site and haven't found exactly what I'm looking for. Close, but no cigar.

Basically I want to have a config section like this:

    <configSections>
        <section name="PhoneNotificationsSection" type="Alerts.PhoneAlertConfigSection,Alerts,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
    </configSections>
    <PhoneNotificationsSection>
        <phones>
            <add phone="MyMobile" value="[email protected]" />
            <add phone="OtherMobile" value="[email protected]" />
        </phones>
    </PhoneNotificationsSection>

Then I'd like to, in my appSettings consuming code, be able to write something like this (pseudo code):

foreach (phone p in phones)
{
   //'phone' attribute is just helpful/descriptive
   DoSomething(p.value);
}

I've done enough research to know I probably need a few of my own classes that implement and/or inherit from certain Configuration classes to make the above code possible. I just haven't found anything that clearly demonstrates this scenario and how to code for it - and when I try to learn the whole .NET configuration world my brain starts to hurt. Anyone have some code like what I'm looking for that they can share?

解决方案

I've written something similar once, as an example for a C# course. In my opinion it mainly demonstrates how awful the .NET configuration subsystem is, although the code does work. I've not adapted it to your settings, as it's fairly easy to introduce a mistake and so far the SO editor does not validate posted code samples ;)

First, the configuration section declaration:

<configSections>
    <section name="passwordSafe"
             type="Codeworks.PasswordSafe.Model.Configuration.PasswordSafeSection, Codeworks.PasswordSafe.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>

<passwordSafe hashAlgorithm="SHA256">
    <users>
        <user name="mm" password="Jok2eyBcFs4y7UIAlCuLix4mLfxw2byfvHfElpmk8d8=" />
        <user name="joe" password="Jok2eyBcFs4y7UIAlCuLix4mLfxw2byfvHfElpmk8d8=" />
    </users>
</passwordSafe>

To match the above snippet we first need the configuration section:

public class PasswordSafeSection : ConfigurationSection
{
    #region Static Accessors
    /// <summary>
    /// Gets the configuration section using the default element name.
    /// </summary>
    public static PasswordSafeSection GetSection()
    {
        return GetSection( "passwordSafe" );
    }

    /// <summary>
    /// Gets the configuration section using the specified element name.
    /// </summary>
    public static PasswordSafeSection GetSection( string sectionName )
    {
        PasswordSafeSection section = ConfigurationManager.GetSection( sectionName ) as PasswordSafeSection;
        if( section == null )
        {
            string message = string.Format( "The specified configuration section (<{0}>) was not found.", sectionName );
            throw new ConfigurationErrorsException( message );
        }
        return section;
    }
    #endregion

    #region Configuration Properties
    [ConfigurationProperty( "hashAlgorithm" )]
    public string HashAlgorithm
    {
        get { return (string) this[ "hashAlgorithm" ]; }
        set { this[ "hashAlgorithm" ] = value; }
    }

    [ConfigurationProperty( "users", IsDefaultCollection=true )]
    public UserElementCollection Users
    {
        get { return (UserElementCollection) this[ "users" ]; }
        set { this[ "users" ] = value; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }
    #endregion
}

We are using a custom element collection, so let's declare that too:

[ConfigurationCollection( typeof(UserElement), CollectionType = ConfigurationElementCollectionType.BasicMap )]
public class UserElementCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new UserElement();
    }

    protected override string ElementName
    {
        get { return "user"; }
    }
    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }

    #region Indexers
    public UserElement this[ int index ]
    {
        get { return BaseGet( index ) as UserElement; }
        set
        {
            if( BaseGet( index ) != null )
            {
                BaseRemoveAt( index );
            }
            BaseAdd( index, value );
        }
    }

    public new UserElement this[ string name ]
    {
        get { return BaseGet( name ) as UserElement; }
    }
    #endregion

    #region Lookup Methods
    protected override object GetElementKey( ConfigurationElement element )
    {
        UserElement user = element as UserElement;
        return user != null ? user.UserName : "error";
    }

    public string GetKey( int index )
    {
        return (string) BaseGetKey( index );
    }
    #endregion

    #region Add/Remove/Clear Methods
    public void Add( UserElement item )
    {
        BaseAdd( item );
    }

    public void Remove( string name )
    {
        BaseRemove( name );
    }

    public void Remove( UserElement item )
    {
        BaseRemove( GetElementKey( item ) );
    }

    public void RemoveAt( int index )
    {
        BaseRemoveAt( index );
    }

    public void Clear()
    {
        BaseClear();
    }
    #endregion
}

And finally we need to declare the custom element used in the element collection:

public class UserElement : ConfigurationElement
{
    #region Constructors
    public UserElement()
    {
    }

    public UserElement( string userName, string passwordHash )
    {
        UserName = userName;
        PasswordHash = passwordHash;
    }
    #endregion

    #region Configuration Properties
    [ConfigurationProperty( "name", IsKey = true )]
    public string UserName
    {
        get { return (string) this[ "name" ]; }
        set { this[ "name" ] = value; }
    }

    [ConfigurationProperty( "password", IsRequired = true )]
    public string PasswordHash
    {
        get { return (string) this[ "password" ]; }
        set { this[ "password" ] = value; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }
    #endregion
}

Now, having all this in place we're ready to access the configuration file. I'm using a Configurator helper class to make this slightly less cumbersome:

public static class Configurator
{
    #region AppSettings Helpers
    public static int SplashScreenDisplayTime
    {
        get { return Convert.ToInt32( ConfigurationManager.AppSettings[ "splash.display.msecs" ] ); }
    }
    #endregion

    #region User Helpers
    public static bool TryGetUserPasswordHash( string userName, out string passwordHash )
    {
        UserElement user = GetUser( userName );
        passwordHash = user != null ? user.PasswordHash : null;
        return ! string.IsNullOrEmpty( passwordHash );
    }

    private static UserElement GetUser( string userName )
    {
        SystemConfiguration config = GetConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal );
        PasswordSafeSection section = config.Sections[ "passwordSafe" ] as PasswordSafeSection;
        return section.Users[ userName ];
    }
    public static void AddUser( string userName, string passwordHash, string encryptionKey )
    {
        SystemConfiguration config = GetConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal );
        PasswordSafeSection section = config.Sections[ "passwordSafe" ] as PasswordSafeSection;
        UserElement user = section.Users[ userName ];
        if( user == null )
        {
            user = new UserElement( userName, passwordHash, encryptionKey );
            section.Users.Add( user );
            config.Save( ConfigurationSaveMode.Modified );
        }
    }
    public static void RemoveUser( string userName )
    {
        SystemConfiguration config = GetConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal );
        PasswordSafeSection section = config.Sections[ "passwordSafe" ] as PasswordSafeSection;
        section.Users.Remove( userName );
        config.Save( ConfigurationSaveMode.Modified );
    }
    public static void UpdateUser( string userName, string passwordHash )
    {
        SystemConfiguration config = GetConfiguration( ConfigurationUserLevel.PerUserRoamingAndLocal );
        PasswordSafeSection section = config.Sections[ "passwordSafe" ] as PasswordSafeSection;
        UserElement user = section.Users[ userName ];
        if( user != null )
        {
            user.PasswordHash = passwordHash;
            config.Save( ConfigurationSaveMode.Modified );
        }
    }
    #endregion

    #region Configuration Helpers
    private static SystemConfiguration GetConfiguration( ConfigurationUserLevel userLevel )
    {
        SystemConfiguration config = InitializeConfiguration( userLevel );
        return config;
    }

    private static SystemConfiguration InitializeConfiguration( ConfigurationUserLevel userLevel )
    {
        SystemConfiguration config = ConfigurationManager.OpenExeConfiguration( userLevel );
        PasswordSafeSection section = config.Sections[ "passwordSafe" ] as PasswordSafeSection;
        if( section == null )
        {
            section = new PasswordSafeSection();
            section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
            section.SectionInformation.ForceSave = true;
            config.Sections.Add( "passwordSafe", section );
            config.Save( ConfigurationSaveMode.Full );
        }
        return config;
    }
    #endregion
}

Hope this helps.

这篇关于代码需要使用foreach对我自己的自定义appSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:52