1
+ using System ;
2
+ using System . Linq ;
3
+ using System . Reflection ;
4
+ using Newtonsoft . Json ;
5
+
6
+ namespace CoreRemoting . Serialization . Bson ;
7
+
8
+ internal class TypeIgnoreVersionConverter : JsonConverter
9
+ {
10
+ public override bool CanConvert ( Type objectType )
11
+ {
12
+ return objectType == typeof ( Type ) ;
13
+ }
14
+
15
+ public override object ReadJson ( JsonReader reader , Type objectType , object existingValue ,
16
+ JsonSerializer serializer )
17
+ {
18
+ if ( reader . TokenType == JsonToken . Null )
19
+ {
20
+ return null ;
21
+ }
22
+
23
+ string typeFullName = reader . Value as string ;
24
+
25
+ Type resultType = GetTypeWithoutVersion ( typeFullName ) ;
26
+
27
+ return resultType ;
28
+ }
29
+
30
+ public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
31
+ {
32
+ writer . WriteValue ( ( ( Type ) value ) ? . AssemblyQualifiedName ) ;
33
+ }
34
+
35
+ private Type GetTypeWithoutVersion ( string fullTypeName )
36
+ {
37
+ var resultType = Type . GetType ( fullTypeName ) ;
38
+
39
+ if ( resultType != null || string . IsNullOrEmpty ( fullTypeName ) )
40
+ return resultType ;
41
+
42
+ var commaIndex = fullTypeName . IndexOf ( ',' ) ;
43
+ if ( commaIndex <= 0 )
44
+ return null ;
45
+ string simpleTypeName = fullTypeName . Substring ( 0 , commaIndex ) . Trim ( ) ;
46
+ string assemblyName = new AssemblyName ( fullTypeName . Substring ( commaIndex + 1 ) . Trim ( ) ) . Name ;
47
+ resultType = FindTypeInLoadedAssemblies ( simpleTypeName , assemblyName ) ;
48
+
49
+ return resultType ;
50
+ }
51
+
52
+ private static Type FindTypeInLoadedAssemblies ( string typeName , string assemblyName )
53
+ {
54
+ return ( from assembly in AppDomain . CurrentDomain . GetAssemblies ( )
55
+ where assembly . FullName != null && assembly . FullName . Contains ( assemblyName )
56
+ select assembly . GetType ( typeName ) ) . FirstOrDefault ( foundType => foundType != null ) ;
57
+ }
58
+ }
0 commit comments