static ObjectContext() { WellKnownContext = new ObjectContext(); // 0 <==> null ulong index = 1; // essential type that must be shared by all serialized stream WellKnownContext.Register(index++, ReflectType.RObject); WellKnownContext.Register(index++, ReflectType.RString); WellKnownContext.Register(index++, ReflectType.RType); WellKnownContext.Register(index++, ReflectType.RReflectType); WellKnownContext.Register(index++, ReflectType.RNullable); // other well known values, to speed up read-write and reduce stream size WellKnownContext.Register(index++, ""); WellKnownContext.Register(index++, ReflectType.GetType(typeof(byte[]))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(Guid))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(bool))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(char))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(byte))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(sbyte))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(short))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(ushort))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(int))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(uint))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(long))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(ulong))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(float))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(double))); WellKnownContext.Register(index++, ReflectType.GetType(typeof(decimal))); }
/// <summary> /// Generates the C# class that can be used to deserialize all given types. /// </summary> /// <param name="namespace">The namespace of the generated class.</param> /// <param name="types">The types that will be rewritten with only the serialization information.</param> /// <returns>A generated C# code file as string.</returns> public static string GenerateCSharpCode(string @namespace, params Type[] types) { var ctxt = new ObjectContext(); foreach (var t in types) { var rt = ReflectType.GetType(t); ctxt.RecursizeAdd(rt); } var sb = new StringBuilder(256); ctxt.GenerateCSharpCode(new StringWriter(sb), @namespace); return(sb.ToString()); }
void WriteCollection <T>(ICollection <T> list) { Writer.Write(list.IsReadOnly); if (list.IsReadOnly) { return; } var count = list.Count; Writer.WriteVInt(count); var surt = ReflectType.GetType(typeof(T)); foreach (var item in list) { count--; Write(surt, item); } if (count != 0) { throw new ArgumentException($"({list}.Count reported an incorrect value ({list.Count})"); } }
void WriteDictionary <K, V>(IDictionary <K, V> list) { Writer.Write(list.IsReadOnly); if (list.IsReadOnly) { return; } var count = list.Count; Writer.WriteVInt(count); var surk = ReflectType.GetType(typeof(K)); var surv = ReflectType.GetType(typeof(V)); foreach (var kv in list) { count--; Write(surk, kv.Key); Write(surv, kv.Value); } if (count != 0) { throw new ArgumentException($"({list}.Count reported an incorrect value ({list.Count})"); } }
internal void Write(ReflectType expected, object o) { // 1st write the ID of the object, return if already written if (expected.IsReference) { ulong oid; var isKnown = Context.TryGetId(o, out oid); if (!isKnown) { oid = Context.NewId(); Context.Register(oid, o); } Writer.WriteVInt(oid); if (isKnown) { return; } } // 2nd write class info, **if needed** var ots = expected; if (!expected.IsFinal) { ots = ReflectType.GetType(o); Write(ReflectType.RReflectType, ots); } // finally write the item if (ots.IsIgnored) { // nothing! } else if (ots == ReflectType.RReflectType) { ((ReflectType)o).Write(this); } else if (ots == ReflectType.RType) { var rf = ReflectType.GetType((Type)o); Write(ReflectType.RReflectType, rf); } else if (ots.IsISerializable && !settings.IgnoreISerializable) { WriteISerializable(ots, o); } else if (ots.HasConverter && !settings.IgnoreTypeConverter) { WriteConverter(ots, o); } else if (ots.HasSurrogate) { BegingSurrogate(ots, o); try { object o2; if (!ots.TryGetSurrogate(o, out o2)) { throw new InvalidOperationException("surrogate failure: couldn't get surrogate instance"); } WriteObject(ots.Surrogate, o2); } finally { EndSurrogate(); } } else { WriteObject(ots, o); } }