Files
LeafWeb/WebCms/Utility/NameValueCollectionUtil.cs
T

31 lines
839 B
C#

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
namespace LeafWeb.WebCms.Utility
{
public static class NameValueCollectionUtil
{
public static NameValueCollection GetNameValueCollection(this object obj, string prefix = "")
{
var nvs = new NameValueCollection();
prefix =
string.IsNullOrEmpty(prefix)
? ""
: prefix + ".";
foreach (var pi in obj.GetType().GetProperties().Where(p => p.CanWrite))
{
var value = pi.GetValue(obj, null);
if (value == null)
continue;
nvs.Add(prefix + pi.Name, value.ToString());
}
return nvs;
}
}
}