public static object UnPack(Type type, byte[] datas) { var obj = System.Activator.CreateInstance(type); var method = type.GetMethod("Add"); var innerType = type.GenericTypeArguments[0]; int dataIndex = 0; while (dataIndex < datas.Length) { var value = FieldFormat.UnPack(innerType, datas, ref dataIndex); method.Invoke(obj, new object[] { value }); } return(obj); }
public static object UnPack(Type type, byte[] datas) { if (addInvoker == null) { var method = type.GetMethod("Add"); addInvoker = DynamicMethodHelper.CreateMethodInvoker(method); } var obj = DynamicMethodHelper.CreateCtorFuncFromCache(type)(); var innerType = type.GenericTypeArguments[0]; int dataIndex = 0; while (dataIndex < datas.Length) { var value = FieldFormat.UnPack(innerType, datas, ref dataIndex); addInvoker.Invoke(obj, new object[] { value }); } return(obj); }
//static Dictionary<Type, TypeInfo> TypeInfoCache = new Dictionary<Type, TypeInfo>(); //static TypeInfo getTypeInfo(Type type) //{ // var a = TypeInfoCache.TryGetValue(type, out TypeInfo typeInfo); // if (!a) // { // var typeRef = typeof(ReflectionHelper); // var method = typeRef.GetMethod(nameof(ReflectionHelper.GetInfo), BindingFlags.Public | BindingFlags.Static); // var refInfo = method.MakeGenericMethod(new Type[] { type }).Invoke(null, new object[] { null }) as IReflectionInfo; // var pro = type.GetProperties().Where(b => b.GetSetMethod() != null); // typeInfo = new TypeInfo() { Properties = pro, ReflectionInfo = refInfo }; // TypeInfoCache.Add(type, typeInfo); // } // return typeInfo; //} public static object UnPack(Type type, byte[] datas) { var obj = DynamicMethodHelper.CreateCtorFuncFromCache(type)(); var typeInfo = type.GetReflectionInfo(); int dataIndex = 0; foreach (var p in typeInfo.Properties) { var value = FieldFormat.UnPack(p.PropertyType, datas, ref dataIndex); if (value == null) { continue; } typeInfo.ReflectionInfo.SetValue(obj, p.Name, value); //p.SetValue(obj, value); } return(obj); }
//static Dictionary<Type, TypeInfo> TypeInfoCache = new Dictionary<Type, TypeInfo>(); //static TypeInfo getTypeInfo(Type type) //{ // var a = TypeInfoCache.TryGetValue(type, out TypeInfo typeInfo); // if (!a) // { // var typeRef = typeof(ReflectionHelper); // var method = typeRef.GetMethod(nameof(ReflectionHelper.GetInfo), BindingFlags.Public | BindingFlags.Static); // var refInfo = method.MakeGenericMethod(new Type[] { type }).Invoke(null, new object[] { null }) as IReflectionInfo; // var pro = type.GetProperties().Where(b => b.GetSetMethod() != null); // typeInfo = new TypeInfo() { Properties = pro, ReflectionInfo = refInfo }; // TypeInfoCache.Add(type, typeInfo); // } // return typeInfo; //} public static object UnPack(Type type, byte[] datas) { var obj = System.Activator.CreateInstance(type); var typeInfo = type.GetReflectionInfo(); int dataIndex = 0; foreach (var p in typeInfo.Properties) { var value = FieldFormat.UnPack(p.PropertyType, datas, ref dataIndex); if (value == null) { continue; } typeInfo.ReflectionInfo.SetValue(obj, p.Name, value); //p.SetValue(obj, value); } return(obj); }
public static object UnPack(Type type, byte[] datas) { var dic = (System.Collections.IDictionary)DynamicMethodHelper.CreateCtorFuncFromCache(type)(); var allArgs = type.GenericTypeArguments; var innerType = allArgs[0]; var innerType2 = allArgs[1]; int dataIndex = 0; while (dataIndex < datas.Length) { var key = FieldFormat.UnPack(innerType, datas, ref dataIndex); var value = FieldFormat.UnPack(innerType2, datas, ref dataIndex); if (key == null) { continue; } dic.Add(key, value); } return(dic); }