处理类:
public string ToString(UserGiftCardModel model)
{
if (model == null) return "";
string sb = "";
Type type =model.GetType() ;//assembly.GetType("Reflect_test.PurchaseOrderHeadManageModel", true, true); //命名空间名称 + 类名
//创建类的实例
//object obj = Activator.CreateInstance(type, true);
//获取公共属性
PropertyInfo[] Propertys = type.GetProperties();
for (int i = 0; i < Propertys.Length; i++)
{
// Propertys[i].SetValue(Propertys[i], i, null); //设置值
PropertyInfo pi = type.GetProperty(Propertys[i].Name);
object val=pi.GetValue(model, null);
string value = val == null ? "" : val.ToString(); ;
sb += Propertys[i].Name +"|"+ value + "_"; //获取值
// Console.WriteLine("属性名:{0},类型:{1}", Propertys[i].Name, Propertys[i].PropertyType);
}
if(sb.Length>0) sb=sb.Substring(0,sb.Length-1);
return sb;
}
public UserGiftCardModel DesToString(string modelstr)
{
if (modelstr == "") return null;
UserGiftCardModel model = new UserGiftCardModel();
Type type = model.GetType();//assembly.GetType("Reflect_test.PurchaseOrderHeadManageModel", true, true); //命名空间名称 + 类名
//创建类的实例
//object obj = Activator.CreateInstance(type, true);
Dictionary<string, object> dict = new Dictionary<string, object>();
//获取公共属性
PropertyInfo[] Propertys = type.GetProperties();
object[] objs = modelstr.Split('_');
foreach (object obj in objs)
{
object[] obj1 = obj.ToString().Split('|');
dict[obj1[0].ToString()] = obj1[1];
}
for (int i = 0; i < Propertys.Length; i++)
{
string p_type= Propertys[i].PropertyType.ToString().ToLower();
if (dict[Propertys[i].Name].ToString() == "") dict[Propertys[i].Name] = null;
// Propertys[i].SetValue(Propertys[i], i, null); //设置值
PropertyInfo pi = type.GetProperty(Propertys[i].Name);
object value=null;
if (p_type.Contains("decimal"))
{
if (dict[Propertys[i].Name] != null)
value = Convert.ToDecimal(dict[Propertys[i].Name]);
}
else if (p_type.Contains("int32"))
{
if (dict[Propertys[i].Name] != null)
value = Convert.ToInt32(dict[Propertys[i].Name]);
}
else if (p_type.Contains("int64"))
{
if (dict[Propertys[i].Name] != null)
value = Convert.ToInt64(dict[Propertys[i].Name]);
}
else if (p_type.Contains("datetime") || p_type.Contains("date"))
{
if (dict[Propertys[i].Name] != null)
value = Convert.ToDateTime(dict[Propertys[i].Name]);
}
else
{
value = dict[Propertys[i].Name];
}
pi.SetValue(model, value, null);
//Console.WriteLine("属性名:{0},类型:{1}", Propertys[i].Name, Propertys[i].PropertyType);
}
return model;
}model类:
public class UserGiftCardModel
{
#region 构造函数
/// <summary>
/// 无参构造函数
/// </summary>
public UserGiftCardModel(){}
/// <summary>
/// 有参构造函数
/// </summary>
/// <param name=" _cardnumber">礼品卡号</param>
/// <param name=" _password">卡密码</param>
/// <param name=" _festivalname">节日名称</param>
/// <param name=" _facevalue">面值</param>
/// <param name=" _balance">余额</param>
/// <param name=" _couponamount">奖励金额</param>
/// <param name=" _picture">图片</param>
/// <param name=" _title">标题 </param>
/// <param name=" _buytime">购买时间</param>
/// <param name=" _buyid">购买人ID</param>
/// <param name=" _recieveid">接收人ID</param>
/// <param name=" _receiveemail">接收者邮箱</param>
/// <param name=" _leavewords">留言</param>
public UserGiftCardModel(string _cardnumber,string _password,string _festivalname,decimal _facevalue,decimal _balance,decimal _couponamount,string _picture,string _title,DateTime _buytime,int _buyid,int _recieveid,string _receiveemail,string _leavewords)
{
this._cardnumber = _cardnumber;
this._password = _password;
this._festivalname = _festivalname;
this._facevalue = _facevalue;
this._balance = _balance;
this._couponamount = _couponamount;
this._picture = _picture;
this._title = _title;
this._buytime = _buytime;
this._buyid = _buyid;
this._recieveid = _recieveid;
this._receiveemail = _receiveemail;
this._leavewords = _leavewords;
}
#endregion
#region 数据库字段对应的属性
private string _cardnumber = "";
/// <summary>
/// 礼品卡号
/// </summary>
public string CardNumber
{
get{ return _cardnumber; }
set{ _cardnumber = value; }
}
private string _password = "";
/// <summary>
/// 卡密码
/// </summary>
public string Password
{
get{ return _password; }
set{ _password = value; }
}
private string _festivalname = "";
/// <summary>
/// 节日名称
/// </summary>
public string FestivalName
{
get{ return _festivalname; }
set{ _festivalname = value; }
}
private decimal? _facevalue = null;
/// <summary>
/// 面值
/// </summary>
public decimal? FaceValue
{
get{ return _facevalue; }
set{ _facevalue = value; }
}
private decimal? _balance = null;
/// <summary>
/// 余额
/// </summary>
public&nbs