public static byte[] Pack(object param) { var list = (System.Collections.IDictionary)param; var type = param.GetType(); var allArgs = type.GenericTypeArguments; var innerType = allArgs[0]; var innerType2 = allArgs[1]; var arry = new List <byte[]>(); var len = 0; var enumerator = list.GetEnumerator(); while (enumerator.MoveNext()) { var key = enumerator.Key; var value = enumerator.Value; var keyData = FieldFormat.Pack(innerType, key); var valueData = FieldFormat.Pack(innerType2, value); arry.Add(keyData); arry.Add(valueData); len += keyData.Length + valueData.Length; } return(arry.JoinData(len)); }
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 byte[] Pack2(Type type, object obj) { var typeInfo = type.GetReflectionInfo(); var arry = new List <byte[]>(); var len = 0; for (int i = 0; i < typeInfo.Properties.Count; i++) { var p = typeInfo.Properties[i]; var value = typeInfo.ReflectionInfo.GetValue(obj, p.Name); var d = FieldFormat.Pack(p.PropertyType, value); arry.Add(d); len += d.Length; } return(arry.JoinData(len)); }
public static byte[] Pack(Type type, object obj) { var typeInfo = type.GetReflectionInfo(); var arry = new List <byte[]>(); var len = 0; foreach (var p in typeInfo.Properties) { var value = typeInfo.ReflectionInfo.GetValue(obj, p.Name); var d = FieldFormat.Pack(p.PropertyType, value); //body.AddRange(d); arry.Add(d); len += d.Length; } //return body.ToArray(); return(arry.JoinData(len)); }
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); }
public static byte[] Pack(Type type, object obj) { var typeInfo = type.GetReflectionInfo(); byte[] data = new byte[0]; var len = 0; for (int i = 0; i < typeInfo.Properties.Count; i++) { var p = typeInfo.Properties[i]; var value = typeInfo.ReflectionInfo.GetValue(obj, p.Name); var d = FieldFormat.Pack(p.PropertyType, value); len += d.Length; Array.Resize(ref data, len); data.Append(len - d.Length, d); } return(data); }
//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 byte[] Pack(object param) { var list = (System.Collections.IEnumerable)param; var body = new List <byte>(); var type = param.GetType(); var innerType = type.GenericTypeArguments[0]; var arry = new List <byte[]>(); var len = 0; foreach (var obj in list) { var data = FieldFormat.Pack(innerType, obj); //body.AddRange(data); arry.Add(data); len += data.Length; } //return body.ToArray(); return(arry.JoinData(len)); }
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); }