Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CecilGenericSerializerFactory.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Linq;
5 using Mono.Cecil;
6 
7 namespace SiliconStudio.AssemblyProcessor
8 {
9  /// <summary>
10  /// Gives the specified serializer type constructed with generic arguments of the serialized type.
11  /// As an example, this instance with <see cref="ListSerializer{}"/> will give <see cref="ListSerializer{int}"/> from <see cref="int"/>.
12  /// </summary>
14  {
15  private readonly Type genericSerializableType;
16  private readonly bool checkInterfaces;
17 
18  protected Func<TypeReference, TypeReference> CreateSerializer { get; set; }
19 
20  private CecilGenericSerializerFactory(Type genericSerializableType, Func<TypeReference, TypeReference> createSerializer)
21  {
22  this.genericSerializableType = genericSerializableType;
23  CreateSerializer = createSerializer;
24  checkInterfaces = genericSerializableType.IsInterface;
25  }
26 
27  public CecilGenericSerializerFactory(Type genericSerializableType, TypeReference genericSerializerType)
28  : this(genericSerializableType, (type => genericSerializerType.MakeGenericType(((GenericInstanceType)type).GenericArguments.ToArray())))
29  {
30  if (genericSerializerType == null)
31  throw new ArgumentNullException("genericSerializerType");
32  if (genericSerializableType == null)
33  throw new ArgumentNullException("genericSerializableType");
34  }
35 
36  #region IDataSerializerFactory Members
37 
38  public virtual TypeReference GetSerializer(TypeReference objectType)
39  {
40  // Check if objectType matches genericSerializableType.
41  // Note: Not perfectly valid but hopefully it should be fast enough.
42  if (objectType.IsGenericInstance && checkInterfaces)
43  {
44  if (objectType.GetElementType().Resolve().Interfaces.Any(x => x.IsGenericInstance && x.GetElementType().FullName == genericSerializableType.FullName))
45  return CreateSerializer(objectType);
46  }
47  if (objectType.IsGenericInstance && objectType.GetElementType().FullName == genericSerializableType.FullName)
48  return CreateSerializer(objectType);
49 
50  return null;
51  }
52 
53  #endregion
54  }
55 }
Gives the specified serializer type constructed with generic arguments of the serialized type...
The generic arguments of the serialized type will be passed as a generic arguments of the serializer...
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
virtual TypeReference GetSerializer(TypeReference objectType)
Gets the serializer type from a given object type.
Gives the required generic serializer for a given type. This is useful for generation of serializatio...
CecilGenericSerializerFactory(Type genericSerializableType, TypeReference genericSerializerType)