1+ using System . Diagnostics ;
12using System . Diagnostics . CodeAnalysis ;
3+ using System . Reflection ;
4+ using Intersect . Framework . Reflection ;
25using Newtonsoft . Json . Serialization ;
36
47namespace Intersect . Framework . Core . Serialization ;
@@ -7,11 +10,44 @@ public sealed class IntersectTypeSerializationBinder : ISerializationBinder
710{
811 private readonly DefaultSerializationBinder _defaultSerializationBinder = new ( ) ;
912
10- public Type BindToType ( string assemblyName , string typeName )
13+ public Type BindToType ( string assemblyName , string qualifiedTypeName )
1114 {
12- return assemblyName . StartsWith ( "Intersect" )
13- ? Type . GetType ( typeName )
14- : _defaultSerializationBinder . BindToType ( assemblyName , typeName ) ;
15+ if ( ! assemblyName . StartsWith ( "Intersect" ) )
16+ {
17+ return _defaultSerializationBinder . BindToType ( assemblyName , qualifiedTypeName ) ;
18+ }
19+
20+ var type = Type . GetType ( qualifiedTypeName ) ;
21+ if ( type is not null )
22+ {
23+ return type ;
24+ }
25+
26+ switch ( assemblyName )
27+ {
28+ case "Intersect Core" :
29+ case "Intersect.Framework.Core" :
30+ break ;
31+ default :
32+ throw new NotImplementedException ( $ "{ nameof ( assemblyName ) } =\" { assemblyName } \" , { nameof ( qualifiedTypeName ) } =\" { qualifiedTypeName } \" ") ;
33+ }
34+
35+ var typeNameSegments = qualifiedTypeName . Split ( '.' ) ;
36+ var typeName = qualifiedTypeName . Split ( '.' ) . Last ( ) ;
37+
38+ var assembly = Assembly . Load ( "Intersect.Framework.Core" ) ;
39+ var types = assembly . GetTypes ( ) ;
40+ var matchingTypes = types . Where ( t => typeName . Equals ( t . Name ) ) . ToArray ( ) ;
41+
42+ return matchingTypes . Length switch
43+ {
44+ 1 => matchingTypes . First ( ) ,
45+ < 1 => throw new InvalidOperationException ( $ "No matching types for { qualifiedTypeName } ") ,
46+ > 2 => throw new InvalidOperationException (
47+ $ "Multiple matching types for { qualifiedTypeName } :\n { string . Join ( "\n " , matchingTypes . Select ( t => $ "\t { t . GetName ( qualified : true ) } ") ) } "
48+ ) ,
49+ _ => throw new UnreachableException ( "There should be no uncovered value" ) ,
50+ } ;
1551 }
1652
1753 public void BindToName (
0 commit comments