using Alchemy.Core.Extensions;
using System.Reflection;
namespace Alchemy.Core.Services
{
public class ConvertService
{
///
/// 字节数组转换为16进制表示的字符串
///
public static string ByteArrayToHexString(byte[] buf)
{
string returnStr = string.Empty;
if (buf.IsNotNull())
{
for (int i = 0; i < buf.Length; i++)
{
returnStr += buf[i].ToString("X2");
}
}
return returnStr;
}
///
/// 类转换成IDic
///
///
///
public static Dictionary ObjToDictionary(object obj)
{
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
Dictionary dict = new Dictionary();
foreach (PropertyInfo property in properties)
{
if (!property.CanRead || !property.CanWrite)
continue;
object value = property.GetValue(obj);
string key = property.Name;
string strValue = value?.ToString();
dict[key] = strValue ?? string.Empty;
}
return dict;
}
}
}