1010 using System ;
1111 using System . Collections . Concurrent ;
1212 using System . Collections . Generic ;
13- using System . Diagnostics . Contracts ;
1413 using System . Linq ;
1514 using System . Reflection ;
1615 using System . Reflection . Emit ;
1918#endif
2019 using static System . Globalization . CultureInfo ;
2120 using static System . Guid ;
21+ using static System . Reflection . BindingFlags ;
2222 using static System . Reflection . Emit . AssemblyBuilderAccess ;
2323
2424 /// <summary>
@@ -69,45 +69,80 @@ IDictionary<EdmTypeKey, TypeInfo> GenerateTypesForEdmModel( IEdmModel model, Api
6969 return ResolveDependencies ( context ) ;
7070 }
7171
72+ static void MapEdmPropertiesToClrProperties (
73+ IEdmModel edmModel ,
74+ IEdmStructuredType edmType ,
75+ Dictionary < string , IEdmProperty > structuralProperties ,
76+ Dictionary < PropertyInfo , IEdmProperty > mappedClrProperties )
77+ {
78+ foreach ( var edmProperty in edmType . Properties ( ) )
79+ {
80+ structuralProperties . Add ( edmProperty . Name , edmProperty ) ;
81+
82+ var clrProperty = edmModel . GetAnnotationValue < ClrPropertyInfoAnnotation > ( edmProperty ) ? . ClrPropertyInfo ;
83+
84+ if ( clrProperty != null )
85+ {
86+ mappedClrProperties . Add ( clrProperty , edmProperty ) ;
87+ }
88+ }
89+ }
90+
7291 static Type GenerateTypeIfNeeded ( IEdmStructuredType structuredType , BuilderContext context )
7392 {
74- var apiVersion = context . ApiVersion ;
75- var edmTypes = context . EdmTypes ;
76- var typeKey = new EdmTypeKey ( structuredType , apiVersion ) ;
93+ var typeKey = new EdmTypeKey ( structuredType , context . ApiVersion ) ;
7794
78- if ( edmTypes . TryGetValue ( typeKey , out var generatedType ) )
95+ if ( context . EdmTypes . TryGetValue ( typeKey , out var generatedType ) )
7996 {
8097 return generatedType ;
8198 }
8299
83- var edmModel = context . EdmModel ;
84- var clrType = structuredType . GetClrType ( edmModel ) ! ;
100+ var clrType = structuredType . GetClrType ( context . EdmModel ) ! ;
85101 var visitedEdmTypes = context . VisitedEdmTypes ;
86102
87103 visitedEdmTypes . Add ( typeKey ) ;
88104
89- const BindingFlags bindingFlags = BindingFlags . Public | BindingFlags . Instance ;
90-
91105 var properties = new List < ClassProperty > ( ) ;
92106 var structuralProperties = new Dictionary < string , IEdmProperty > ( StringComparer . OrdinalIgnoreCase ) ;
93107 var mappedClrProperties = new Dictionary < PropertyInfo , IEdmProperty > ( ) ;
94- var clrTypeMatchesEdmType = true ;
95- var hasUnfinishedTypes = false ;
96108 var dependentProperties = new List < PropertyDependency > ( ) ;
97109
98- foreach ( var property in structuredType . Properties ( ) )
99- {
100- structuralProperties . Add ( property . Name , property ) ;
101-
102- var clrProperty = edmModel . GetAnnotationValue < ClrPropertyInfoAnnotation > ( property ) ? . ClrPropertyInfo ;
110+ MapEdmPropertiesToClrProperties ( context . EdmModel , structuredType , structuralProperties , mappedClrProperties ) ;
111+
112+ var ( clrTypeMatchesEdmType , hasUnfinishedTypes ) =
113+ BuildSignatureProperties (
114+ clrType ,
115+ structuralProperties ,
116+ mappedClrProperties ,
117+ properties ,
118+ dependentProperties ,
119+ context ) ;
120+
121+ return ResolveType (
122+ typeKey ,
123+ clrType ,
124+ clrTypeMatchesEdmType ,
125+ hasUnfinishedTypes ,
126+ properties ,
127+ dependentProperties ,
128+ context ) ;
129+ }
103130
104- if ( clrProperty != null )
105- {
106- mappedClrProperties . Add ( clrProperty , property ) ;
107- }
108- }
131+ static Tuple < bool , bool > BuildSignatureProperties (
132+ Type clrType ,
133+ IReadOnlyDictionary < string , IEdmProperty > structuralProperties ,
134+ IReadOnlyDictionary < PropertyInfo , IEdmProperty > mappedClrProperties ,
135+ List < ClassProperty > properties ,
136+ List < PropertyDependency > dependentProperties ,
137+ BuilderContext context )
138+ {
139+ var edmModel = context . EdmModel ;
140+ var apiVersion = context . ApiVersion ;
141+ var visitedEdmTypes = context . VisitedEdmTypes ;
142+ var clrTypeMatchesEdmType = true ;
143+ var hasUnfinishedTypes = false ;
109144
110- foreach ( var property in clrType . GetProperties ( bindingFlags ) )
145+ foreach ( var property in clrType . GetProperties ( Public | Instance ) )
111146 {
112147 if ( ! structuralProperties . TryGetValue ( property . Name , out var structuralProperty ) &&
113148 ! mappedClrProperties . TryGetValue ( property , out structuralProperty ) )
@@ -178,6 +213,21 @@ static Type GenerateTypeIfNeeded( IEdmStructuredType structuredType, BuilderCont
178213 properties . Add ( new ClassProperty ( property , propertyType ) ) ;
179214 }
180215
216+ return Tuple . Create ( clrTypeMatchesEdmType , hasUnfinishedTypes ) ;
217+ }
218+
219+ static TypeInfo ResolveType (
220+ EdmTypeKey typeKey ,
221+ Type clrType ,
222+ bool clrTypeMatchesEdmType ,
223+ bool hasUnfinishedTypes ,
224+ List < ClassProperty > properties ,
225+ List < PropertyDependency > dependentProperties ,
226+ BuilderContext context )
227+ {
228+ var apiVersion = context . ApiVersion ;
229+ var edmTypes = context . EdmTypes ;
230+
181231 TypeInfo type ;
182232
183233 if ( clrTypeMatchesEdmType )
@@ -240,6 +290,7 @@ static TypeBuilder CreateTypeBuilderFromSignature( ModuleBuilder moduleBuilder,
240290 ref var property = ref properties [ i ] ;
241291 var type = property . Type ;
242292 var name = property . Name ;
293+
243294 AddProperty ( typeBuilder , type , name , property . Attributes ) ;
244295 }
245296
@@ -253,15 +304,15 @@ static IDictionary<EdmTypeKey, TypeInfo> ResolveDependencies( BuilderContext con
253304
254305 for ( var i = 0 ; i < dependencies . Count ; i ++ )
255306 {
256- var propertyDependency = dependencies [ i ] ;
257- var dependentOnType = edmTypes [ propertyDependency . DependentOnTypeKey ] ;
307+ var dependency = dependencies [ i ] ;
308+ var dependentOnType = edmTypes [ dependency . DependentOnTypeKey ] ;
258309
259- if ( propertyDependency . IsCollection )
310+ if ( dependency . IsCollection )
260311 {
261312 dependentOnType = IEnumerableOfT . MakeGenericType ( dependentOnType ) . GetTypeInfo ( ) ;
262313 }
263314
264- AddProperty ( propertyDependency . DependentType ! , dependentOnType , propertyDependency . PropertyName , propertyDependency . CustomAttributes ) ;
315+ AddProperty ( dependency . DependentType ! , dependentOnType , dependency . PropertyName , dependency . CustomAttributes ) ;
265316 }
266317
267318 var keys = edmTypes . Keys . ToArray ( ) ;
@@ -338,7 +389,7 @@ internal BuilderContext( IEdmModel edmModel, ApiVersion apiVersion, Func<ModuleB
338389
339390 internal IDictionary < EdmTypeKey , TypeInfo > EdmTypes { get ; } = new Dictionary < EdmTypeKey , TypeInfo > ( ) ;
340391
341- internal ICollection < EdmTypeKey > VisitedEdmTypes { get ; } = new HashSet < EdmTypeKey > ( ) ;
392+ internal ISet < EdmTypeKey > VisitedEdmTypes { get ; } = new HashSet < EdmTypeKey > ( ) ;
342393
343394 internal IList < PropertyDependency > Dependencies { get ; } = new List < PropertyDependency > ( ) ;
344395 }
0 commit comments