@@ -95,16 +95,27 @@ static IResource CreateResource(JObject jObj, Type resourceType)
9595 foreach ( var prop in resourceType . GetProperties ( ) . Where ( p => Representation . IsEmbeddedResourceType ( p . PropertyType ) ) )
9696 {
9797 // expects embedded collection of resources is implemented as an IList on the Representation-derived class
98- var lst = prop . GetValue ( resource ) as IList ;
99- if ( lst != null )
98+ if ( typeof ( IEnumerable < IResource > ) . IsAssignableFrom ( prop . PropertyType ) )
10099 {
100+ var lst = prop . GetValue ( resource ) as IList ;
101+ if ( lst == null )
102+ {
103+ lst = ConstructResource ( prop . PropertyType ) as IList ??
104+ Activator . CreateInstance (
105+ typeof ( List < > ) . MakeGenericType ( prop . PropertyType . GenericTypeArguments ) ) as IList ;
106+ if ( lst == null ) continue ;
107+ prop . SetValue ( resource , lst ) ;
108+ }
101109 if ( prop . PropertyType . GenericTypeArguments != null &&
102110 prop . PropertyType . GenericTypeArguments . Length > 0 )
103111 CreateEmbedded ( embeddeds , prop . PropertyType . GenericTypeArguments [ 0 ] ,
104112 newRes => lst . Add ( newRes ) ) ;
105113 }
106114 else
107- CreateEmbedded ( embeddeds , prop . PropertyType , newRes => prop . SetValue ( resource , newRes ) ) ;
115+ {
116+ var prop1 = prop ;
117+ CreateEmbedded ( embeddeds , prop . PropertyType , newRes => prop1 . SetValue ( resource , newRes ) ) ;
118+ }
108119 }
109120 }
110121
@@ -177,22 +188,7 @@ static string GetResourceTypeRel(Type resourceType)
177188 {
178189 if ( ResourceTypeToRel . ContainsKey ( resourceType . FullName ) )
179190 return ResourceTypeToRel [ resourceType . FullName ] ;
180- // favor c-tor with zero params, but if it doesn't exist, use c-tor with fewest params and pass all null values
181- var ctors = resourceType . GetConstructors ( ) ;
182- ConstructorInfo useThisCtor = null ;
183- foreach ( var ctor in ctors )
184- {
185- if ( ctor . GetParameters ( ) . Length == 0 )
186- {
187- useThisCtor = ctor ;
188- break ;
189- }
190- if ( useThisCtor == null || useThisCtor . GetParameters ( ) . Length > ctor . GetParameters ( ) . Length )
191- useThisCtor = ctor ;
192- }
193- if ( useThisCtor == null ) return string . Empty ;
194- var ctorParams = new object [ useThisCtor . GetParameters ( ) . Length ] ;
195- var res = useThisCtor . Invoke ( ctorParams ) as IResource ;
191+ var res = ConstructResource ( resourceType ) as IResource ;
196192 if ( res != null )
197193 {
198194 var rel = res . Rel ;
@@ -208,6 +204,26 @@ static string GetResourceTypeRel(Type resourceType)
208204 }
209205 }
210206
207+ static object ConstructResource ( Type resourceType )
208+ {
209+ // favor c-tor with zero params, but if it doesn't exist, use c-tor with fewest params and pass all null values
210+ var ctors = resourceType . GetConstructors ( ) ;
211+ ConstructorInfo useThisCtor = null ;
212+ foreach ( var ctor in ctors )
213+ {
214+ if ( ctor . GetParameters ( ) . Length == 0 )
215+ {
216+ useThisCtor = ctor ;
217+ break ;
218+ }
219+ if ( useThisCtor == null || useThisCtor . GetParameters ( ) . Length > ctor . GetParameters ( ) . Length )
220+ useThisCtor = ctor ;
221+ }
222+ if ( useThisCtor == null ) return null ;
223+ var ctorParams = new object [ useThisCtor . GetParameters ( ) . Length ] ;
224+ return useThisCtor . Invoke ( ctorParams ) ;
225+ }
226+
211227 public override bool CanConvert ( Type objectType )
212228 {
213229 return IsResource ( objectType ) && ! IsResourceList ( objectType ) ;
0 commit comments