MakeSerializer_Monsajem_Array() { var Type = typeof(t); var ItemsSerializer = GetSerialize(System.Array.CreateInstance( ((Collection.Array.Base.IArray)GetUninitializedObject(Type)).ElementType, 0).GetType()); var ObjSerializer = SerializeInfo <object> .GetSerialize(); Action <SerializeData, object> Serializer = (Data, obj) => { var ar = (Collection.Array.Base.IArray)obj; ObjSerializer.Serializer(Data, ar.Comparer); ObjSerializer.Serializer(Data, ar.MyOptions); ItemsSerializer.Serializer(Data, ar.ToArray()); }; Func <DeserializeData, object> Deserializer = (Data) => { var ar = (Collection.Array.Base.IArray)GetUninitializedObject(Type); ar.Comparer = ObjSerializer.Deserializer(Data); ar.MyOptions = ObjSerializer.Deserializer(Data); ar.Insert((Array)ItemsSerializer.Deserializer(Data)); return(ar); }; return(Serializer, Deserializer); }
private SerializeInfo WriteSerializer(SerializeData Data, Type Type) { var Sr = SerializeInfo.GetSerialize(Type); VisitedInfoSerialize <object>(Data, Sr.Type, () => (Sr.NameAsByte, null)); return(Sr); }
private SerializeInfo ReadSerializer(DeserializeData Data) { var Info = VisitedInfoDeserialize(Data, () => { return(Read(Data)); }); return(SerializeInfo.GetSerialize(Info)); }
private byte[] _Serialize <t>(t obj, Action <Type> TrustToType, Action <MethodInfo> TrustToMethod) { byte[] Result; var SR = SerializeInfo <t> .GetSerialize(); SerializeData SR_Data = default; try { #if DEBUG if (Deletage_Target == null) { var Fields = DynamicAssembly.FieldControler.GetFields(typeof(Delegate)); var Fields_str = ""; for (int i = 0; i < Fields.Length; i++) { Fields_str += "\n" + Fields[i].Name; } throw new Exception("Cant Access to Deletage Target field at serializer, Fields >>" + Fields_str); } #endif var ConstantSize = SR.ConstantSize; if (ConstantSize == -1) { SR_Data = new SerializeData(true, TrustToType, TrustToMethod); VisitedSerialize(SR_Data, obj, SR); Result = SR_Data.Data.ToArray(); } else { //return StructToBytes(obj, ConstantSize); SR_Data = new SerializeData(false, TrustToType, TrustToMethod, new MemoryStream(ConstantSize)); SR.Serializer(SR_Data, obj); Result = SR_Data.Data.ToArray(); } } #if DEBUG catch (Exception ex) { var Traced = SR_Data.Traced; if (Traced != null) { Traced = "On " + Traced; } SR_Data.Traced = null; throw new Exception($"Serialize Of Type >> {obj.GetType().FullName} Is Failed " + Traced, ex); } #endif finally { } return(Result); }
public t Deserialize <t>(byte[] Data, ref int From, Action <Type> TrustToType, Action <MethodInfo> TrustToMethod) { t Result = default; var SR = SerializeInfo <t> .GetSerialize(); DeserializeData DR_Data = default; try { #if DEBUG if (Deletage_Target == null) { var Fields = DynamicAssembly.FieldControler.GetFields(typeof(Delegate)); var Fields_str = ""; for (int i = 0; i < Fields.Length; i++) { Fields_str += "\n" + Fields[i].Name; } throw new Exception("Cant Access to Deletage Target field at serializer, Fields >>" + Fields_str); } #endif if (SR.ConstantSize == -1) { DR_Data = new DeserializeData(true, TrustToType, TrustToMethod, Data); VisitedDeserialize(DR_Data, (c) => Result = (t)c, SR); DR_Data.AtLast?.Invoke(); } else { //return BytesToStruct<t>(Data, 0); DR_Data = new DeserializeData(false, TrustToType, TrustToMethod, Data); Result = (t)SR.Deserializer(DR_Data); } } #if DEBUG catch (Exception ex) { var Traced = DR_Data.Traced; if (Traced != null) { Traced = "On " + Traced; } DR_Data.Traced = null; throw new Exception($"Deserialize From Point {DR_Data.From} Of Type >> {typeof(t).FullName} Is Failed {Traced}\nDatas As B64:\n" + System.Convert.ToBase64String(Data), ex); } #endif finally { } return(Result); }
private byte[] _Serialize <t>(t obj, Action <Type> TrustToType) { byte[] Result; var SR = SerializeInfo <t> .GetSerialize(); var SR_Data = new SerializeData() { Trust = TrustToType }; try { #if DEBUG if (Deletage_Target == null) { var Fields = DynamicAssembly.FieldControler.GetFields(typeof(Delegate)); var Fields_str = ""; for (int i = 0; i < Fields.Length; i++) { Fields_str += "\n" + Fields[i].Name; } throw new Exception("Cant Access to Deletage Target field at serializer, Fields >>" + Fields_str); } #endif VisitedSerialize(SR_Data, obj, SR); Result = SR_Data.S_Data.ToArray(); } #if DEBUG catch (Exception ex) { var Traced = SR_Data.Traced; if (Traced != null) { Traced = "On " + Traced; } SR_Data.Traced = null; throw new Exception($"Serialize Of Type >> {obj.GetType().FullName} Is Failed " + Traced, ex); } #endif finally { } return(Result); }
public Serialization() { Serializere = this; SerializeInfo <object> .InsertSerializer( (Data, obj) => { }, (Data) => new object(), false); SerializeInfo <bool> .InsertSerializer( (Data, obj) => { if ((bool)obj == true) { Data.S_Data.WriteByte(1); } else { Data.S_Data.WriteByte(0); } }, (Data) => { int Position = Data.From; Data.From += 1; var Result = (Data.D_Data)[Position]; return(Result > 0); }, true); SerializeInfo <char> .InsertSerializer( (Data, Obj) => { Data.S_Data.Write(BitConverter.GetBytes((char)Obj), 0, 2); }, (Data) => { int Position = Data.From; Data.From += 2; return(BitConverter.ToChar(Data.D_Data, Position)); }, true); SerializeInfo <byte> .InsertSerializer( (Data, Obj) => { Data.S_Data.WriteByte((byte)Obj); }, (Data) => { int Position = Data.From; Data.From += 1; return(Data.D_Data[Position]); }, true); SerializeInfo <sbyte> .InsertSerializer( (Data, Obj) => { Data.S_Data.WriteByte((byte)((sbyte)Obj)); }, (Data) => { int Position = Data.From; Data.From += 1; return((sbyte)Data.D_Data[Position]); }, true); SerializeInfo <short> .InsertSerializer( (Data, obj) => { Data.S_Data.Write(BitConverter.GetBytes((short)obj), 0, 2); }, (Data) => { int Position = Data.From; Data.From += 2; return(BitConverter.ToInt16(Data.D_Data, Position)); }, true); SerializeInfo <ushort> .InsertSerializer( (Data, Obj) => { Data.S_Data.Write(BitConverter.GetBytes((ushort)Obj), 0, 2); }, (Data) => { /// as UInt16 int Position = Data.From; Data.From += 2; return(BitConverter.ToUInt16(Data.D_Data, Position)); }, true); SerializeInfo <int> .InsertSerializer( (Data, obj) => { /// as Int32 Data.S_Data.Write(BitConverter.GetBytes((int)obj), 0, 4); }, (Data) => { /// as Int32 int Position = Data.From; Data.From += 4; return(BitConverter.ToInt32(Data.D_Data, Position)); }, true); SerializeInfo <uint> .InsertSerializer( (Data, obj) => { /// as UInt32 Data.S_Data.Write(BitConverter.GetBytes((uint)obj), 0, 4); }, (Data) => { /// as UInt32 int Position = Data.From; Data.From += 4; return(BitConverter.ToUInt32(Data.D_Data, Position)); }, true); SerializeInfo <long> .InsertSerializer( (Data, obj) => { /// as Int64 Data.S_Data.Write(BitConverter.GetBytes((long)obj), 0, 8); }, (Data) => { /// as Int64 int Position = Data.From; Data.From += 8; return(BitConverter.ToInt64(Data.D_Data, Position)); }, true); SerializeInfo <ulong> .InsertSerializer( (Data, obj) => { Data.S_Data.Write(BitConverter.GetBytes((ulong)obj), 0, 8); }, (Data) => { int Position = Data.From; Data.From += 8; return(BitConverter.ToUInt64(Data.D_Data, Position)); }, true); SerializeInfo <float> .InsertSerializer( (Data, obj) => { /// as float Data.S_Data.Write(BitConverter.GetBytes((float)obj), 0, 4); }, (Data) => { /// as float int Position = Data.From; Data.From += 4; return(BitConverter.ToSingle(Data.D_Data, Position)); }, true); SerializeInfo <double> .InsertSerializer( (Data, obj) => { /// as double Data.S_Data.Write(BitConverter.GetBytes((double)obj), 0, 8); }, (Data) => { /// as double int Position = Data.From; Data.From += 8; return(BitConverter.ToDouble(Data.D_Data, Position)); }, true); SerializeInfo <DateTime> .InsertSerializer( (Data, obj) => { Data.S_Data.Write(BitConverter.GetBytes(((DateTime)obj).Ticks), 0, 8); }, (Data) => { int Position = Data.From; Data.From += 8; return(DateTime.FromBinary(BitConverter.ToInt64(Data.D_Data, Position))); }, true); SerializeInfo <string> .InsertSerializer( (Data, obj) => { if (obj == null) { Data.S_Data.Write(BitConverter.GetBytes(-1), 0, 4); return; } var Str = UTF8.GetBytes((string)obj); var Len = BitConverter.GetBytes(Str.Length); Data.S_Data.Write(Len, 0, 4); Data.S_Data.Write(Str, 0, Str.Length); }, (Data) => { var StrSize = BitConverter.ToInt32(Data.D_Data, Data.From); Data.From += 4; if (StrSize == -1) { return(null); } var Position = Data.From; Data.From += StrSize; return(UTF8.GetString(Data.D_Data, Position, StrSize)); }, true); SerializeInfo <IntPtr> .InsertSerializer( (Data, obj) => { /// as IntPtr Data.S_Data.Write(BitConverter.GetBytes(((IntPtr)obj).ToInt64()), 0, 8); }, (Data) => { /// as IntPtr int Position = Data.From; Data.From += 8; return(new IntPtr(BitConverter.ToInt64(Data.D_Data, Position))); }, true); SerializeInfo <UIntPtr> .InsertSerializer( (Data, obj) => { /// as UIntPtr Data.S_Data.Write(BitConverter.GetBytes(((UIntPtr)obj).ToUInt64()), 0, 8); }, (Data) => { /// as UIntPtr int Position = Data.From; Data.From += 8; return(new UIntPtr(BitConverter.ToUInt64(Data.D_Data, Position))); }, true); SerializeInfo <decimal> .InsertSerializer( (Data, obj) => { /// as Decimal Data.S_Data.Write(BitConverter.GetBytes(Decimal.ToDouble((decimal)obj)), 0, 8); }, (Data) => { /// as Decimal int Position = Data.From; Data.From += 8; return(System.Convert.ToDecimal(BitConverter.ToDouble(Data.D_Data, Position))); }, true); SerializeInfo <Type> .InsertSerializer( (Data, obj) => { var Name = Write(((Type)obj).MidName()); Data.S_Data.Write(Name, 0, Name.Length); }, (Data) => { return(Read(Data).GetTypeByName()); }, true); { var SR = SerializeInfo <object> .GetSerialize(); SerializeInfo <System.Runtime.InteropServices.GCHandle> .InsertSerializer( (Data, obj) => { var GC = (System.Runtime.InteropServices.GCHandle)obj; SR.Serializer(Data, GC.Target); }, (Data) => { return(System.Runtime.InteropServices.GCHandle.Alloc(SR.Deserializer(Data))); }, true); } SerializeInfo <IEqualityComparer <string> > .InsertSerializer( (Data, obj) => { if (obj == null) { Data.S_Data.WriteByte(0); } else { Data.S_Data.WriteByte(1); } }, (Data) => { if (Data.D_Data[Data.From++] == 0) { return(null); } else { return(EqualityComparer <string> .Default); } }, true); }
public override void Make() { var Type = typeof(t); var IsSerializable = Type.GetInterfaces().Where( (c) => { if (c.IsGenericType) { if (c.GetGenericTypeDefinition() == ISerializableType) { return(true); } } return(false); }).FirstOrDefault(); if (IsSerializable != null) { InsertSerializer(() => { var sr = MakeSerializer_Serializable(); return(sr.Serializer, sr.Deserializer); }); } else if (Type == typeof(Delegate)) { InsertSerializer( (Data, obj) => { var DType = obj.GetType(); var Serializer = GetSerialize(DType); Serializere.VisitedInfoSerialize <object>(Data, DType, () => (Serializer.NameAsByte, null)); Serializer.Serializer(Data, obj); }, (Data) => { var Info = Serializere.VisitedInfoDeserialize(Data, () => Serializere.Read(Data)); return(GetSerialize(Info).Deserializer(Data)); }); } else if (Type.GetInterfaces().Where((c) => c == typeof(Collection.Array.Base.IArray)).FirstOrDefault() != null) { InsertSerializer(() => { var sr = MakeSerializer_Monsajem_Array(); return(sr.Serializer, sr.Deserializer); }); } else if (Type.IsInterface) { InsertSerializer(() => { var Sr = SerializeInfo <object> .GetSerialize(); return(Sr.Serializer, Sr.Deserializer); }); } else if (Type.IsArray) { var size = SerializeInfo.GetSerialize(Type.GetElementType()).ConstantSize; if (size != -1) { InsertSerializer(() => { var sr = MakeSerializer_Array_Struct(size); return(sr.Serializer, sr.Deserializer); }); } else { InsertSerializer(() => { var sr = MakeSerializer_Array_Class(); return(sr.Serializer, sr.Deserializer); }); } } else if (Type == typeof(System.Array)) { InsertSerializer(() => { var Sr = SerializeInfo <object> .GetSerialize(); return(Sr.Serializer, Sr.Deserializer); }); } else if (Type == typeof(System.Array)) { InsertSerializer(() => { var sr = MakeSerializer_Array_Class(); return((Data, obj) => { var ar = (System.Array)obj; }, (Data) => { return null; } ); }); } else if (Type.BaseType == typeof(MulticastDelegate)) { InsertSerializer(() => { var sr = MakeSerializer_Delegate(); return(sr.Serializer, sr.Deserializer); }); } else if (Nullable.GetUnderlyingType(Type) != null) { InsertSerializer(() => { var sr = MakeSerializer_Nullable(); return(sr.Serializer, sr.Deserializer); }); } else if (Type.IsValueType) { if (ConstantSize == -1) { InsertSerializer(() => { var sr = MakeSerializer_ValueType(); return(sr.Serializer, sr.Deserializer); }); } else { InsertSerializer(() => (Default_Serializer, Default_Deserializer)); } } else { InsertSerializer(() => { var sr = MakeSerializer_Else(); return(sr.Serializer, sr.Deserializer); }); } }
public Serialization() { Serializere = this; SerializeInfo <object> .InsertSerializer( (Data, obj) => { }, (Data) => new object(), false); SerializeInfo <DateTime> .InsertSerializer( (Data, obj) => { Data.Data.Write(BitConverter.GetBytes(((DateTime)obj).Ticks), 0, 8); }, (Data) => { int Position = Data.From; Data.From += 8; return(DateTime.FromBinary(BitConverter.ToInt64(Data.Data, Position))); }, true); SerializeInfo <string> .InsertSerializer( (Data, obj) => { if (obj == null) { Data.Data.Write(BitConverter.GetBytes(-1), 0, 4); return; } var Str = UTF8.GetBytes((string)obj); var Len = BitConverter.GetBytes(Str.Length); Data.Data.Write(Len, 0, 4); Data.Data.Write(Str, 0, Str.Length); }, (Data) => { var StrSize = BitConverter.ToInt32(Data.Data, Data.From); Data.From += 4; if (StrSize == -1) { return(null); } var Position = Data.From; Data.From += StrSize; return(UTF8.GetString(Data.Data, Position, StrSize)); }, true); SerializeInfo <Type> .InsertSerializer( (Data, obj) => { var Name = Write((obj as Type).MidName()); Data.Data.Write(Name, 0, Name.Length); }, (Data) => { return(Read(Data).GetTypeByName()); }, true); { var SR = SerializeInfo <object> .GetSerialize(); SerializeInfo <System.Runtime.InteropServices.GCHandle> .InsertSerializer( (Data, obj) => { var GC = (System.Runtime.InteropServices.GCHandle)obj; SR.Serializer(Data, GC.Target); }, (Data) => { return(System.Runtime.InteropServices.GCHandle.Alloc(SR.Deserializer(Data))); }, true); } SerializeInfo <IEqualityComparer <string> > .InsertSerializer( (Data, obj) => { if (obj == null) { Data.Data.WriteByte(0); } else { Data.Data.WriteByte(1); } }, (Data) => { if (Data.Data[Data.From++] == 0) { return(null); } else { return(EqualityComparer <string> .Default); } }, true); }
public int SizeOf <t>() => SerializeInfo <t> .GetSerialize().ConstantSize;