/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Everest.Library.Reflection;
namespace Everest.Library.ExtensionMethod{
public static class NameValueCollectionExtensions
{
/// <summary>
/// Initializes the <see cref="NameValueCollectionEx"/> class.
/// </summary>
static NameValueCollectionExtensions()
{
}
/// <summary>
/// Sets the read only.
/// </summary>
/// <param name="nameObjectCol">The name object col.</param>
/// <param name="readOnly">if set to <c>true</c> [read only].</param>
public static void SetReadOnly(this NameObjectCollectionBase nameObjectCol, bool readOnly)
{
typeof(NameValueCollection).SetPropertyValue("IsReadOnly", nameObjectCol, readOnly);
}
/// <summary>
/// Serializes the name value collection to XML.
/// </summary>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
/// <param name="dic">The dic.</param>
/// <returns></returns>
public static string SerializeNameValueCollectionToXml(this NameValueCollection nameValues)
{
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
foreach (var key in nameValues.AllKeys)
{
list.Add(new KeyValuePair<string, string>(key, nameValues[key]));
}
return list.SerializeToXml();
}
/// <summary>
/// Deserializes the name value collection from XML string.
/// </summary>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
/// <param name="xml">The XML.</param>
/// <returns></returns>
public static NameValueCollection DeserializeNameValueCollectionFromXmlString(string xml)
{
List<KeyValuePair<string, string>> list = ObjectExtensions.DeserializeFromXmlString<List<KeyValuePair<string, string>>>(xml);
NameValueCollection nameValues = new NameValueCollection();
foreach (var item in list)
{
nameValues.Add(item.Key, item.Value);
}
return nameValues;
}
/// <summary>
/// Toes the string.
/// </summary>
/// <param name="nameValues">The name values.</param>
/// <returns></returns>
public static string ToKeyValueString(this NameValueCollection nameValues)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (string key in nameValues.Keys)
{
stringBuilder.AppendFormat("{0}:{1},", key, nameValues[key]);
}
stringBuilder.RemoveLastSpecifiedChar(',');
return stringBuilder.ToString();
}
public static IDictionary<string, object> ToDictionary(this NameValueCollection nameValues)
{
if (nameValues == null)
{
return null;
}
IDictionary<string, object> dic = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
foreach (string key in nameValues.Keys)
{
dic[key] = nameValues[key];
}
return dic;
}
}
}
|