1+ using System ;
2+ using System . Linq ;
3+ using System . Reflection ;
4+
15namespace ReactiveXComponent . Serializer
26{
37 public static class SerializerFactory
48 {
9+ private static ISerializer _customSerializer ;
10+ private static readonly object CustomSerializerLock = new object ( ) ;
11+
512 public static ISerializer CreateSerializer ( SerializationType serializationType )
613 {
714 switch ( serializationType )
@@ -12,9 +19,53 @@ public static ISerializer CreateSerializer(SerializationType serializationType)
1219 return new JsonSerializer ( ) ;
1320 case SerializationType . Bson :
1421 return new BsonSerializer ( ) ;
22+ case SerializationType . Custom :
23+ return FindCustomSerializer ( ) ;
1524 default :
1625 throw new XCSerializationException ( "Unhandled serialization type " + serializationType ) ;
1726 }
1827 }
28+
29+ private static ISerializer FindCustomSerializer ( )
30+ {
31+ if ( _customSerializer == null )
32+ {
33+ lock ( CustomSerializerLock )
34+ {
35+ if ( _customSerializer == null )
36+ {
37+ ISerializer customSerializer = null ;
38+
39+ foreach ( var assembly in AppDomain . CurrentDomain . GetAssemblies ( ) )
40+ {
41+ if ( assembly . GetCustomAttributes ( ) . OfType < CustomSerializerContainerAttribute > ( ) . Any ( ) )
42+ {
43+ foreach ( var exportedType in assembly . GetExportedTypes ( ) )
44+ {
45+ var customSerializerAttribute = exportedType . GetCustomAttributes ( )
46+ . OfType < CustomSerializerAttribute > ( )
47+ . FirstOrDefault ( ) ;
48+
49+ if ( customSerializerAttribute != null )
50+ {
51+ customSerializer = ( ISerializer ) Activator . CreateInstance ( exportedType ) ;
52+ break ;
53+ }
54+ }
55+ }
56+ }
57+
58+ if ( customSerializer == null )
59+ {
60+ throw new XCSerializationException ( "Unable to find a custom serializer" ) ;
61+ }
62+
63+ _customSerializer = customSerializer ;
64+ }
65+ }
66+ }
67+
68+ return _customSerializer ;
69+ }
1970 }
2071}
0 commit comments