JSON反序列化到实体

应朋友要求写了一个反序列化JSON的东东
因为系统自带Json转换方法的无法将""转换DateTime?

初略写了一下 有任何问题或者建议 请留言。

using System;
using System.Reflection.Emit;
using System.Reflection;

public class JSONSerialize<T>
{
    public JSONSerialize()
    {
    }
    private delegate T Delegate(string json);
    private static Delegate handler;
    public static T DeSerialize(string json)
    {

        
        DynamicMethod method = new DynamicMethod("DynamicDeSerialize", typeof(T), new Type[] { typeof(string) }, typeof(T), true);
        ILGenerator generator = method.GetILGenerator();
   
        LocalBuilder result = generator.DeclareLocal(typeof(T));
        generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
        generator.Emit(OpCodes.Stloc, result);
        json = json.Substring(1, json.Length - 2);
        string[] str = json.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string item in str)
        {
            string[] strpro = item.Split(new string[] { "\":\"" }, StringSplitOptions.None);
            string name = strpro[0].Replace("\"", "");
            string value = strpro[1].Replace("\"", "");
           
            PropertyInfo propertyInfo = typeof(T).GetProperty(name);
            LocalBuilder temp = generator.DeclareLocal(propertyInfo.PropertyType);
            Type t = typeof(Decimal);;
            MethodInfo dateParse= typeof(Decimal).GetMethod("Parse", new Type[] { typeof(string) });
         
            if (propertyInfo.PropertyType == typeof(Nullable<Int32>))
            {
                t = typeof(Int32);
                dateParse = t.GetMethod("Parse", new Type[] { typeof(string) });
            }
            else if (propertyInfo.PropertyType == typeof(Nullable<DateTime>))
            {
                t = typeof(DateTime);
                dateParse = t.GetMethod("Parse", new Type[] { typeof(string) });
            }

            if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
            {
                generator.Emit(OpCodes.Ldloc, result);
                if (propertyInfo.PropertyType==typeof(String))
                {
                    generator.Emit(OpCodes.Ldstr, value);
                    generator.Emit(OpCodes.Stloc, temp);
                    generator.Emit(OpCodes.Ldloc, temp);
                    generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                    generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());

                }
                else
                {
                    if (value != "")
                    {
                        generator.Emit(OpCodes.Ldstr, value);
                        generator.Emit(OpCodes.Call, dateParse);
                        generator.Emit(OpCodes.Newobj, propertyInfo.PropertyType.GetConstructor(new Type[] { t }));
                        generator.Emit(OpCodes.Stloc, temp);
                        generator.Emit(OpCodes.Ldloc, temp);
                        generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
                    }
                }
            }
        }
        generator.Emit(OpCodes.Ldloc, result);
        generator.Emit(OpCodes.Ret);
        handler = (Delegate)method.CreateDelegate(typeof(Delegate));
        return handler(json);
    }
}

 

posted @ 2013-05-31 16:04  丫的  阅读(1326)  评论(3编辑  收藏  举报