SACenter/SA.Core/Util/DictionaryHelper.cs

53 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SA.Core.Util
{
public class DictionaryHelper
{
public static string GetkvString(Dictionary<string, string> keys)
{
StringBuilder str = new();
foreach (var item in keys)
{
str.Append(string.Format("{0}={1}", item.Key, item.Value));
str.Append("&");
}
return str.ToString().TrimEnd('&');
}
public static string GetJsonString(Dictionary<string, string> keys)
{
StringBuilder str = new();
foreach (var item in keys)
{
str.Append(string.Format("\"{0}\":\"{1}\"", item.Key, item.Value));
str.Append(",");
}
return str.ToString().TrimEnd(',');
}
public static Dictionary<string, string> GetKeyValues<T>(T model )
{
Dictionary<string, string> keys = new();
if( model == null ) return keys;
Type t= model.GetType();
var list = t.GetProperties().ToList();
foreach (var item in list)
{
var value = item.GetValue(model);
if (value == null) value="";
keys.TryAdd(item.Name, value.ToString() ?? "");
}
return keys;
}
}
}