using System;
using System.Collections.Generic;
using System.Text;
namespace PackDLL.Data.ConvertData
{
/// <summary>
/// 实体类、字符串互相转换
/// </summary>
public class PackReflectionEntity<T>
{
/// <summary>
/// 将实体类通过反射组装成字符串
/// </summary>
/// <param name="t">实体类</param>
/// <returns>组装的字符串</returns>
public static string GetEntityToString(T t)
{
System.Text.StringBuilder sb = new StringBuilder();
Type type = t.GetType();
System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
for (int i = 0; i < propertyInfos.Length; i++)
{
sb.Append(propertyInfos[i].Name + ":" + propertyInfos[i].GetValue(t, null) + ",");
}
return sb.ToString().TrimEnd(new char[] { ',' });
}
/// <summary>
/// 将反射得到字符串转换为对象
/// </summary>
/// <param name="str">反射得到的字符串</param>
/// <returns>实体类</returns>
public static T GetEntityStringToEntity(string str)
{
string[] array = str.Split(',');
string[] temp = null;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (string s in array)
{
temp = s.Split(':');
dictionary.Add(temp[0], temp[1]);
}
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(T));
T entry = (T)assembly.CreateInstance(typeof(T).FullName);
System.Text.StringBuilder sb = new StringBuilder();
Type type = entry.GetType();
System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
for (int i = 0; i < propertyInfos.Length; i++)
{
foreach (string key in dictionary.Keys)
{
if (propertyInfos[i].Name == key.ToString())
{
propertyInfos[i].SetValue(entry, GetObject(propertyInfos[i], dictionary[key]), null);
break;
}
}
}
return entry;
}
/// <summary>
/// 转换值的类型
/// </summary>
/// <param name="p"></param>
/// <param name="value"></param>
/// <returns></returns>
{
string name = p.PropertyType.FullName.ToString().ToLower();
if (value == "")
return null;
if (name.Contains("int16"))
{
return value.ConvertTo<Int16>();
}
else if (name.Contains("int32"))
{
return value.ConvertTo<Int32>();
}
else if (name.Contains("int64"))
{
return value.ConvertTo<Int64>();
}
else if (name.Contains("byte"))
{
return value.ConvertTo<Byte>();
}
else if (name.Contains("datetime"))
{
return value.ConvertTo<DateTime>();
}
else if (name.Contains("decimal"))
{
return value.ConvertTo<Decimal>();
}
else if (name.Contains("boolean"))
{
return value.ConvertTo<Boolean>();
}
else if (name.Contains("char"))
{
return value.ConvertTo<char>();
}
else if (name.Contains("single"))
{
return value.ConvertTo<float>();
}
else if (name.Contains("double"))
{
return value.ConvertTo<double>();
}
else if (name.Contains("string"))
{
return value.ConvertTo<string>();
}
else
{
return value;
}
}
}
public static class ConvertionExtensions
{
public static T ConvertTo<T>(this IConvertible convertibleValue)
{
if (null == convertibleValue)
{
return default(T);
}
if (!typeof(T).IsGenericType)
{
return (T)Convert.ChangeType(convertibleValue, typeof(T));
}
else
{
Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
return (T)Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof(T)));
}
}
throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, typeof(T).FullName));
}
}
{
public static T ConvertTo<T>(this IConvertible convertibleValue)
{
if (null == convertibleValue)
{
return default(T);
}
if (!typeof(T).IsGenericType)
{
return (T)Convert.ChangeType(convertibleValue, typeof(T));
}
else
{
Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
return (T)Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof(T)));
}
}
throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, typeof(T).FullName));
}
}
}
}