From f67c201f45a9965f2c92fc4eb1149a460a3c8474 Mon Sep 17 00:00:00 2001 From: jkuehner Date: Sat, 14 Nov 2015 12:36:58 +0100 Subject: [PATCH] Make Methods in Serializers virtual so they can be overwritten in derived classes --- NetSerializer/TypeSerializers/ArraySerializer.cs | 8 ++++---- .../TypeSerializers/DictionarySerializer.cs | 8 ++++---- NetSerializer/TypeSerializers/EnumSerializer.cs | 8 ++++---- .../TypeSerializers/GenericSerializer.cs | 16 ++++++---------- NetSerializer/TypeSerializers/NoOpSerializer.cs | 8 ++++---- .../TypeSerializers/NullableSerializer.cs | 8 ++++---- .../TypeSerializers/ObjectSerializer.cs | 8 ++++---- 7 files changed, 30 insertions(+), 34 deletions(-) diff --git a/NetSerializer/TypeSerializers/ArraySerializer.cs b/NetSerializer/TypeSerializers/ArraySerializer.cs index a69887a..afa9685 100644 --- a/NetSerializer/TypeSerializers/ArraySerializer.cs +++ b/NetSerializer/TypeSerializers/ArraySerializer.cs @@ -17,7 +17,7 @@ namespace NetSerializer { class ArraySerializer : IDynamicTypeSerializer { - public bool Handles(Type type) + public virtual bool Handles(Type type) { if (!type.IsArray) return false; @@ -28,12 +28,12 @@ public bool Handles(Type type) return true; } - public IEnumerable GetSubtypes(Type type) + public virtual IEnumerable GetSubtypes(Type type) { return new[] { typeof(uint), type.GetElementType() }; } - public void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator il) + public virtual void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator il) { var elemType = type.GetElementType(); @@ -105,7 +105,7 @@ public void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator i il.Emit(OpCodes.Ret); } - public void GenerateReaderMethod(Serializer serializer, Type type, ILGenerator il) + public virtual void GenerateReaderMethod(Serializer serializer, Type type, ILGenerator il) { var elemType = type.GetElementType(); diff --git a/NetSerializer/TypeSerializers/DictionarySerializer.cs b/NetSerializer/TypeSerializers/DictionarySerializer.cs index b061ced..fac4e2a 100644 --- a/NetSerializer/TypeSerializers/DictionarySerializer.cs +++ b/NetSerializer/TypeSerializers/DictionarySerializer.cs @@ -18,7 +18,7 @@ namespace NetSerializer { public class DictionarySerializer : IStaticTypeSerializer { - public bool Handles(Type type) + public virtual bool Handles(Type type) { if (!type.IsGenericType) return false; @@ -28,7 +28,7 @@ public bool Handles(Type type) return genTypeDef == typeof(Dictionary<,>); } - public IEnumerable GetSubtypes(Type type) + public virtual IEnumerable GetSubtypes(Type type) { // Dictionary is stored as KeyValuePair[] @@ -39,7 +39,7 @@ public IEnumerable GetSubtypes(Type type) return new[] { serializedType }; } - public MethodInfo GetStaticWriter(Type type) + public virtual MethodInfo GetStaticWriter(Type type) { Debug.Assert(type.IsGenericType); @@ -61,7 +61,7 @@ public MethodInfo GetStaticWriter(Type type) return writer; } - public MethodInfo GetStaticReader(Type type) + public virtual MethodInfo GetStaticReader(Type type) { Debug.Assert(type.IsGenericType); diff --git a/NetSerializer/TypeSerializers/EnumSerializer.cs b/NetSerializer/TypeSerializers/EnumSerializer.cs index cf626ca..a98444a 100644 --- a/NetSerializer/TypeSerializers/EnumSerializer.cs +++ b/NetSerializer/TypeSerializers/EnumSerializer.cs @@ -18,19 +18,19 @@ namespace NetSerializer { public class EnumSerializer : IStaticTypeSerializer { - public bool Handles(Type type) + public virtual bool Handles(Type type) { return type.IsEnum; } - public IEnumerable GetSubtypes(Type type) + public virtual IEnumerable GetSubtypes(Type type) { var underlyingType = Enum.GetUnderlyingType(type); return new[] { underlyingType }; } - public MethodInfo GetStaticWriter(Type type) + public virtual MethodInfo GetStaticWriter(Type type) { Debug.Assert(type.IsEnum); @@ -39,7 +39,7 @@ public MethodInfo GetStaticWriter(Type type) return Primitives.GetWritePrimitive(underlyingType); } - public MethodInfo GetStaticReader(Type type) + public virtual MethodInfo GetStaticReader(Type type) { Debug.Assert(type.IsEnum); diff --git a/NetSerializer/TypeSerializers/GenericSerializer.cs b/NetSerializer/TypeSerializers/GenericSerializer.cs index 0fcdb6c..c215acb 100644 --- a/NetSerializer/TypeSerializers/GenericSerializer.cs +++ b/NetSerializer/TypeSerializers/GenericSerializer.cs @@ -7,19 +7,15 @@ */ using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; +using System.Collections.Generic; using System.Reflection; -using System.Reflection.Emit; -using System.Text; +using System.Reflection.Emit; namespace NetSerializer { public class GenericSerializer : IDynamicTypeSerializer { - public bool Handles(Type type) + public virtual bool Handles(Type type) { if (!type.IsSerializable) throw new NotSupportedException(String.Format("Type {0} is not marked as Serializable", type.FullName)); @@ -30,7 +26,7 @@ public bool Handles(Type type) return true; } - public IEnumerable GetSubtypes(Type type) + public virtual IEnumerable GetSubtypes(Type type) { var fields = Helpers.GetFieldInfos(type); @@ -38,7 +34,7 @@ public IEnumerable GetSubtypes(Type type) yield return field.FieldType; } - public void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator il) + public virtual void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator il) { // arg0: Serializer, arg1: Stream, arg2: value @@ -68,7 +64,7 @@ public void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator i il.Emit(OpCodes.Ret); } - public void GenerateReaderMethod(Serializer serializer, Type type, ILGenerator il) + public virtual void GenerateReaderMethod(Serializer serializer, Type type, ILGenerator il) { // arg0: Serializer, arg1: stream, arg2: out value diff --git a/NetSerializer/TypeSerializers/NoOpSerializer.cs b/NetSerializer/TypeSerializers/NoOpSerializer.cs index 72b5c03..88489d0 100644 --- a/NetSerializer/TypeSerializers/NoOpSerializer.cs +++ b/NetSerializer/TypeSerializers/NoOpSerializer.cs @@ -29,7 +29,7 @@ public NoOpSerializer(IEnumerable types, bool handleSubclasses) m_handleSubclasses = handleSubclasses; } - public bool Handles(Type type) + public virtual bool Handles(Type type) { if (m_handleSubclasses) return m_types.Any(t => type.IsSubclassOf(t)); @@ -37,17 +37,17 @@ public bool Handles(Type type) return m_types.Contains(type); } - public IEnumerable GetSubtypes(Type type) + public virtual IEnumerable GetSubtypes(Type type) { return new Type[0]; } - public MethodInfo GetStaticWriter(Type type) + public virtual MethodInfo GetStaticWriter(Type type) { return this.GetType().GetMethod("Serialize", BindingFlags.Static | BindingFlags.Public); } - public MethodInfo GetStaticReader(Type type) + public virtual MethodInfo GetStaticReader(Type type) { return this.GetType().GetMethod("Deserialize", BindingFlags.Static | BindingFlags.Public); } diff --git a/NetSerializer/TypeSerializers/NullableSerializer.cs b/NetSerializer/TypeSerializers/NullableSerializer.cs index 9964cb2..ffd47f9 100644 --- a/NetSerializer/TypeSerializers/NullableSerializer.cs +++ b/NetSerializer/TypeSerializers/NullableSerializer.cs @@ -15,7 +15,7 @@ namespace NetSerializer { public class NullableSerializer : IDynamicTypeSerializer { - public bool Handles(Type type) + public virtual bool Handles(Type type) { if (!type.IsGenericType) return false; @@ -25,14 +25,14 @@ public bool Handles(Type type) return genTypeDef == typeof(Nullable<>); } - public IEnumerable GetSubtypes(Type type) + public virtual IEnumerable GetSubtypes(Type type) { var genArgs = type.GetGenericArguments(); return new[] { typeof(bool), genArgs[0] }; } - public void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator il) + public virtual void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator il) { var valueType = type.GetGenericArguments()[0]; @@ -66,7 +66,7 @@ public void GenerateWriterMethod(Serializer serializer, Type type, ILGenerator i il.Emit(OpCodes.Ret); } - public void GenerateReaderMethod(Serializer serializer, Type type, ILGenerator il) + public virtual void GenerateReaderMethod(Serializer serializer, Type type, ILGenerator il) { var valueType = type.GetGenericArguments()[0]; diff --git a/NetSerializer/TypeSerializers/ObjectSerializer.cs b/NetSerializer/TypeSerializers/ObjectSerializer.cs index 2ca28a6..2f84ccf 100644 --- a/NetSerializer/TypeSerializers/ObjectSerializer.cs +++ b/NetSerializer/TypeSerializers/ObjectSerializer.cs @@ -16,22 +16,22 @@ namespace NetSerializer { class ObjectSerializer : IStaticTypeSerializer { - public bool Handles(Type type) + public virtual bool Handles(Type type) { return type == typeof(object); } - public IEnumerable GetSubtypes(Type type) + public virtual IEnumerable GetSubtypes(Type type) { return new Type[0]; } - public MethodInfo GetStaticWriter(Type type) + public virtual MethodInfo GetStaticWriter(Type type) { return typeof(ObjectSerializer).GetMethod("Serialize", BindingFlags.Static | BindingFlags.Public); } - public MethodInfo GetStaticReader(Type type) + public virtual MethodInfo GetStaticReader(Type type) { return typeof(ObjectSerializer).GetMethod("Deserialize", BindingFlags.Static | BindingFlags.Public); }