日期:2014-05-17  浏览次数:20887 次

c#利用反射,实现将model类变成字符串、再还原成mode对象的功能
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;
        }



	public class UserGiftCardModel
	{
		#region 构造函数
		/// <summary>
		/// 无参构造函数 
		/// </summary>
		public UserGiftCardModel(){}
		/// <summary>
        /// 有参构造函数