7
7
using System ;
8
8
using System . Collections . Generic ;
9
9
using System . Linq ;
10
- using System . Reflection ;
11
- #if WEBAPI
12
- using System . Web . Http . Dispatcher ;
13
- #endif
14
- using static System . Globalization . CultureInfo ;
15
- using static System . String ;
16
- #if ! WEBAPI
17
- using static System . StringComparison ;
18
- #endif
10
+ using System . Runtime . CompilerServices ;
19
11
20
12
static class EdmExtensions
21
13
{
14
+ const bool ThrowOnError = true ;
15
+
22
16
internal static Type ? GetClrType ( this IEdmType edmType , IEdmModel edmModel )
23
17
{
24
18
if ( edmType is not IEdmSchemaType schemaType )
@@ -27,15 +21,13 @@ static class EdmExtensions
27
21
}
28
22
29
23
var typeName = schemaType . FullName ( ) ;
30
- var type = DeriveFromWellKnowPrimitive ( typeName ) ;
31
24
32
- if ( type != null )
25
+ if ( DeriveFromWellKnowPrimitive ( typeName ) is Type type )
33
26
{
34
27
return type ;
35
28
}
36
29
37
- var element = ( IEdmSchemaType ) edmType ;
38
- var annotationValue = edmModel . GetAnnotationValue < ClrTypeAnnotation > ( element ) ;
30
+ var annotationValue = edmModel . GetAnnotationValue < ClrTypeAnnotation > ( schemaType ) ;
39
31
40
32
if ( annotationValue != null )
41
33
{
@@ -45,49 +37,29 @@ static class EdmExtensions
45
37
return null ;
46
38
}
47
39
48
- static Type ? DeriveFromWellKnowPrimitive ( string edmFullName )
40
+ static Type ? DeriveFromWellKnowPrimitive ( string edmFullName ) => edmFullName switch
49
41
{
50
- switch ( edmFullName )
51
- {
52
- case "Edm.String" :
53
- case "Edm.Byte" :
54
- case "Edm.SByte" :
55
- case "Edm.Int16" :
56
- case "Edm.Int32" :
57
- case "Edm.Int64" :
58
- case "Edm.Double" :
59
- case "Edm.Single" :
60
- case "Edm.Boolean" :
61
- case "Edm.Decimal" :
62
- case "Edm.DateTime" :
63
- case "Edm.DateTimeOffset" :
64
- case "Edm.Guid" :
65
- #if WEBAPI
66
- return Type . GetType ( edmFullName . Replace ( "Edm" , "System" ) , throwOnError : true ) ;
67
- #else
68
- return Type . GetType ( edmFullName . Replace ( "Edm" , "System" , Ordinal ) , throwOnError : true ) ;
69
- #endif
70
- case "Edm.Duration" :
71
- return typeof ( TimeSpan ) ;
72
- case "Edm.Binary" :
73
- return typeof ( byte [ ] ) ;
74
- case "Edm.Geography" :
75
- case "Edm.Geometry" :
76
- #if WEBAPI
77
- return Type . GetType ( edmFullName . Replace ( "Edm" , "Microsoft.Spatial" ) , throwOnError : true ) ;
78
- #else
79
- return Type . GetType ( edmFullName . Replace ( "Edm" , "Microsoft.Spatial" , Ordinal ) , throwOnError : true ) ;
80
- #endif
81
- case "Edm.Date" :
82
- case "Edm.TimeOfDay" :
42
+ "Edm.String" or "Edm.Byte" or "Edm.SByte" or "Edm.Int16" or "Edm.Int32" or "Edm.Int64" or
43
+ "Edm.Double" or "Edm.Single" or "Edm.Boolean" or "Edm.Decimal" or "Edm.DateTime" or "Edm.DateTimeOffset" or
44
+ "Edm.Guid" => Type . GetType ( Requalify ( edmFullName , "System" ) , ThrowOnError ) ,
45
+ "Edm.Duration" => typeof ( TimeSpan ) ,
46
+ "Edm.Binary" => typeof ( byte [ ] ) ,
47
+ "Edm.Geography" or "Edm.Geometry" => GetTypeFromAssembly ( edmFullName , "Microsoft.Spatial" ) ,
48
+ "Edm.Date" or "Edm.TimeOfDay" => GetTypeFromAssembly ( edmFullName , "Microsoft.OData.Edm" ) ,
49
+ _ => null ,
50
+ } ;
51
+
52
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
83
53
#if WEBAPI
84
- return Type . GetType ( edmFullName . Replace ( "Edm" , "Microsoft.OData.Edm" ) , throwOnError : true ) ;
54
+ static string Requalify ( string edmFullName , string @namespace ) => @namespace + edmFullName . Substring ( 3 ) ;
85
55
#else
86
- return Type . GetType ( edmFullName . Replace ( "Edm" , "Microsoft.OData.Edm" , Ordinal ) , throwOnError : true ) ;
56
+ static string Requalify ( string edmFullName , string @namespace ) => string . Concat ( @namespace . AsSpan ( ) , edmFullName . AsSpan ( ) . Slice ( 3 ) ) ;
87
57
#endif
88
- }
89
58
90
- return null ;
59
+ static Type ? GetTypeFromAssembly ( string edmFullName , string assemblyName )
60
+ {
61
+ var typeName = Requalify ( edmFullName , assemblyName ) + "," + assemblyName ;
62
+ return Type . GetType ( typeName , ThrowOnError ) ;
91
63
}
92
64
}
93
65
}
0 commit comments