1+ using System ;
2+ using System . Globalization ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Reflection ;
6+
7+ namespace GridDomain . Common
8+ {
9+ public class LegacyWireSerializer
10+ {
11+ private readonly object _serializer ;
12+ private readonly MethodInfo _serializeMethod ;
13+ private readonly MethodInfo _deserializeMethod ;
14+
15+ public LegacyWireSerializer ( )
16+ {
17+ var path = Path . Combine ( Path . GetDirectoryName ( Assembly . GetAssembly ( typeof ( LegacyWireSerializer ) ) . Location ) ,
18+ @"LegacyBinaries\LegacyWire_0.0.6.dll" ) ;
19+
20+ if ( ! File . Exists ( path ) )
21+ throw new CannotFindLegacyWireLibraryException ( ) ;
22+
23+ var assembly = Assembly . LoadFile ( path ) ;
24+
25+ var options = CreateByConstructor ( assembly , "Wire.SerializerOptions" , new object [ ] { true , null , false , null } ) ;
26+ _serializer = CreateByConstructor ( assembly , "Wire.Serializer" , new [ ] { options } ) ;
27+
28+ _serializeMethod = _serializer . GetType ( ) . GetMethod ( "Serialize" , new [ ] { typeof ( object ) , typeof ( Stream ) } ) ;
29+ if ( _serializeMethod == null )
30+ throw new MissingMethodException ( "Cannot find serialize method for legacy wire" ) ;
31+
32+ _deserializeMethod = _serializer . GetType ( ) . GetMethods ( ) . FirstOrDefault ( m => m . Name == "Deserialize" & ! m . IsGenericMethod ) ;
33+ if ( _deserializeMethod == null )
34+ throw new MissingMethodException ( "Cannot find deserialize method for legacy wire" ) ;
35+ }
36+
37+ private static object CreateByConstructor ( Assembly assembly , string typeName , object [ ] parameters )
38+ {
39+ return assembly . CreateInstance ( typeName ,
40+ true ,
41+ BindingFlags . Public | BindingFlags . Instance ,
42+ null ,
43+ parameters ,
44+ CultureInfo . CurrentCulture ,
45+ null ) ;
46+ }
47+
48+ public object Deserialize ( byte [ ] payload , Type type )
49+ {
50+ using ( var stream = new MemoryStream ( payload ) )
51+ return _deserializeMethod . Invoke ( _serializer , new object [ ] { stream } ) ;
52+ }
53+
54+ public byte [ ] Serialize ( object obj )
55+ {
56+ using ( var stream = new MemoryStream ( ) )
57+ {
58+ _serializeMethod . Invoke ( _serializer , new object [ ] { obj , stream } ) ;
59+ return stream . ToArray ( ) ;
60+ }
61+ }
62+ }
63+
64+ public class CannotFindLegacyWireLibraryException : Exception
65+ {
66+ }
67+ }
0 commit comments