Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Methods in Serializers virtual so they can be overwritten in der… #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions NetSerializer/TypeSerializers/ArraySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,12 +28,12 @@ public bool Handles(Type type)
return true;
}

public IEnumerable<Type> GetSubtypes(Type type)
public virtual IEnumerable<Type> 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();

Expand Down Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions NetSerializer/TypeSerializers/DictionarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +28,7 @@ public bool Handles(Type type)
return genTypeDef == typeof(Dictionary<,>);
}

public IEnumerable<Type> GetSubtypes(Type type)
public virtual IEnumerable<Type> GetSubtypes(Type type)
{
// Dictionary<K,V> is stored as KeyValuePair<K,V>[]

Expand All @@ -39,7 +39,7 @@ public IEnumerable<Type> GetSubtypes(Type type)
return new[] { serializedType };
}

public MethodInfo GetStaticWriter(Type type)
public virtual MethodInfo GetStaticWriter(Type type)
{
Debug.Assert(type.IsGenericType);

Expand All @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions NetSerializer/TypeSerializers/EnumSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type> GetSubtypes(Type type)
public virtual IEnumerable<Type> 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);

Expand All @@ -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);

Expand Down
16 changes: 6 additions & 10 deletions NetSerializer/TypeSerializers/GenericSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -30,15 +26,15 @@ public bool Handles(Type type)
return true;
}

public IEnumerable<Type> GetSubtypes(Type type)
public virtual IEnumerable<Type> GetSubtypes(Type type)
{
var fields = Helpers.GetFieldInfos(type);

foreach (var field in fields)
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

Expand Down Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions NetSerializer/TypeSerializers/NoOpSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ public NoOpSerializer(IEnumerable<Type> 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));
else
return m_types.Contains(type);
}

public IEnumerable<Type> GetSubtypes(Type type)
public virtual IEnumerable<Type> 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);
}
Expand Down
8 changes: 4 additions & 4 deletions NetSerializer/TypeSerializers/NullableSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,14 +25,14 @@ public bool Handles(Type type)
return genTypeDef == typeof(Nullable<>);
}

public IEnumerable<Type> GetSubtypes(Type type)
public virtual IEnumerable<Type> 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];

Expand Down Expand Up @@ -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];

Expand Down
8 changes: 4 additions & 4 deletions NetSerializer/TypeSerializers/ObjectSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type> GetSubtypes(Type type)
public virtual IEnumerable<Type> 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);
}
Expand Down