77 using System ;
88 using System . Collections . Generic ;
99 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 ;
1911
2012 static class EdmExtensions
2113 {
14+ const bool ThrowOnError = true ;
15+
2216 internal static Type ? GetClrType ( this IEdmType edmType , IEdmModel edmModel )
2317 {
2418 if ( edmType is not IEdmSchemaType schemaType )
@@ -27,15 +21,13 @@ static class EdmExtensions
2721 }
2822
2923 var typeName = schemaType . FullName ( ) ;
30- var type = DeriveFromWellKnowPrimitive ( typeName ) ;
3124
32- if ( type != null )
25+ if ( DeriveFromWellKnowPrimitive ( typeName ) is Type type )
3326 {
3427 return type ;
3528 }
3629
37- var element = ( IEdmSchemaType ) edmType ;
38- var annotationValue = edmModel . GetAnnotationValue < ClrTypeAnnotation > ( element ) ;
30+ var annotationValue = edmModel . GetAnnotationValue < ClrTypeAnnotation > ( schemaType ) ;
3931
4032 if ( annotationValue != null )
4133 {
@@ -45,49 +37,29 @@ static class EdmExtensions
4537 return null ;
4638 }
4739
48- static Type ? DeriveFromWellKnowPrimitive ( string edmFullName )
40+ static Type ? DeriveFromWellKnowPrimitive ( string edmFullName ) => edmFullName switch
4941 {
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 ) ]
8353#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 ) ;
8555#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 ) ) ;
8757#endif
88- }
8958
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 ) ;
9163 }
9264 }
9365}
0 commit comments