@@ -168,16 +168,17 @@ private static Delegate CreateReadDelegate(
168168 var method = new DynamicMethod (
169169 $ "{ DynamicMethodPrefix } Read{ elementType . Name } ",
170170 typeof ( void ) ,
171- // reader, namespaceUri
172171 new [ ] { typeof ( XmlReader ) , typeof ( string ) , elementType } ,
173172 elementType . Module ) ;
174173 ILGenerator generator = method . GetILGenerator ( ) ;
175174
175+ // Ldarg_0 = reader
176+ // Ldarg_1 = namespace URI
177+ // Ldarg_2 = element
178+
176179 generator . DeclareLocal ( typeof ( string ) ) ;
177180
178- generator . Emit ( OpCodes . Ldarg_0 ) ;
179- generator . Emit ( OpCodes . Ldstr , "key" ) ;
180- EmitCall ( generator , Metadata . GetAttributeMethod ) ;
181+ GetXmlAttribute ( "key" ) ;
181182 generator . Emit ( OpCodes . Stloc_0 ) ;
182183
183184 // We need to create the switch for each property
@@ -210,7 +211,7 @@ private static Delegate CreateReadDelegate(
210211 if ( setMethod is null )
211212 throw new InvalidOperationException ( $ "Property { property . DeclaringType } .{ property . Name } has no setter.") ;
212213
213- // reader.ReadXXX + SetField
214+ // element.xxx = reader.ReadXXX
214215 generator . Emit ( OpCodes . Ldarg_2 ) ; // element
215216 generator . Emit ( OpCodes . Ldarg_0 ) ; // reader
216217 generator . Emit ( OpCodes . Ldstr , "data" ) ;
@@ -233,6 +234,17 @@ private static Delegate CreateReadDelegate(
233234
234235 // Let's bake the method
235236 return method . CreateDelegate ( delegateType ) ;
237+
238+ #region Local function
239+
240+ void GetXmlAttribute ( string attributeName )
241+ {
242+ generator . Emit ( OpCodes . Ldarg_0 ) ;
243+ generator . Emit ( OpCodes . Ldstr , attributeName ) ;
244+ EmitCall ( generator , Metadata . GetAttributeMethod ) ;
245+ }
246+
247+ #endregion
236248 }
237249 }
238250
@@ -368,28 +380,23 @@ private void ReadElements()
368380 }
369381 }
370382
371- private void ReadEdge ( [ NotNull ] IDictionary < string , TVertex > vertices )
383+ private void ReadVertex ( [ NotNull ] IDictionary < string , TVertex > vertices )
372384 {
373385 Debug . Assert ( vertices != null ) ;
374386 Debug . Assert (
375387 _reader . NodeType == XmlNodeType . Element
376- && _reader . Name == EdgeTag
388+ && _reader . Name == NodeTag
377389 && _reader . NamespaceURI == _graphMLNamespace ) ;
378390
379391 // Get subtree
380392 using ( XmlReader subReader = _reader . ReadSubtree ( ) )
381393 {
382394 // Read id
383395 string id = ReadAttributeValue ( _reader , IdAttribute ) ;
384- string sourceId = ReadAttributeValue ( _reader , SourceAttribute ) ;
385- if ( ! vertices . TryGetValue ( sourceId , out TVertex source ) )
386- throw new ArgumentException ( $ "Could not find vertex { sourceId } .") ;
387- string targetId = ReadAttributeValue ( _reader , TargetAttribute ) ;
388- if ( ! vertices . TryGetValue ( targetId , out TVertex target ) )
389- throw new ArgumentException ( $ "Could not find vertex { targetId } .") ;
390-
391- TEdge edge = _edgeFactory ( source , target , id ) ;
392- ReadDelegateCompiler . SetEdgeDefault ( edge ) ;
396+ // Create new vertex
397+ TVertex vertex = _vertexFactory ( id ) ;
398+ // Apply defaults
399+ ReadDelegateCompiler . SetVertexDefault ( vertex ) ;
393400
394401 // Read data
395402 while ( subReader . Read ( ) )
@@ -398,31 +405,38 @@ private void ReadEdge([NotNull] IDictionary<string, TVertex> vertices)
398405 && subReader . Name == DataTag
399406 && subReader . NamespaceURI == _graphMLNamespace )
400407 {
401- ReadDelegateCompiler . EdgeAttributesReader ( subReader , _graphMLNamespace , edge ) ;
408+ ReadDelegateCompiler . VertexAttributesReader ( subReader , _graphMLNamespace , vertex ) ;
402409 }
403410 }
404411
405- _graph . AddEdge ( edge ) ;
412+ // Add to graph
413+ _graph . AddVertex ( vertex ) ;
414+ vertices . Add ( id , vertex ) ;
406415 }
407416 }
408417
409- private void ReadVertex ( [ NotNull ] IDictionary < string , TVertex > vertices )
418+ private void ReadEdge ( [ NotNull ] IDictionary < string , TVertex > vertices )
410419 {
411420 Debug . Assert ( vertices != null ) ;
412421 Debug . Assert (
413422 _reader . NodeType == XmlNodeType . Element
414- && _reader . Name == NodeTag
423+ && _reader . Name == EdgeTag
415424 && _reader . NamespaceURI == _graphMLNamespace ) ;
416425
417426 // Get subtree
418427 using ( XmlReader subReader = _reader . ReadSubtree ( ) )
419428 {
420429 // Read id
421430 string id = ReadAttributeValue ( _reader , IdAttribute ) ;
422- // Create new vertex
423- TVertex vertex = _vertexFactory ( id ) ;
424- // Apply defaults
425- ReadDelegateCompiler . SetVertexDefault ( vertex ) ;
431+ string sourceId = ReadAttributeValue ( _reader , SourceAttribute ) ;
432+ if ( ! vertices . TryGetValue ( sourceId , out TVertex source ) )
433+ throw new ArgumentException ( $ "Could not find vertex { sourceId } .") ;
434+ string targetId = ReadAttributeValue ( _reader , TargetAttribute ) ;
435+ if ( ! vertices . TryGetValue ( targetId , out TVertex target ) )
436+ throw new ArgumentException ( $ "Could not find vertex { targetId } .") ;
437+
438+ TEdge edge = _edgeFactory ( source , target , id ) ;
439+ ReadDelegateCompiler . SetEdgeDefault ( edge ) ;
426440
427441 // Read data
428442 while ( subReader . Read ( ) )
@@ -431,13 +445,11 @@ private void ReadVertex([NotNull] IDictionary<string, TVertex> vertices)
431445 && subReader . Name == DataTag
432446 && subReader . NamespaceURI == _graphMLNamespace )
433447 {
434- ReadDelegateCompiler . VertexAttributesReader ( subReader , _graphMLNamespace , vertex ) ;
448+ ReadDelegateCompiler . EdgeAttributesReader ( subReader , _graphMLNamespace , edge ) ;
435449 }
436450 }
437451
438- // Add to graph
439- _graph . AddVertex ( vertex ) ;
440- vertices . Add ( id , vertex ) ;
452+ _graph . AddEdge ( edge ) ;
441453 }
442454 }
443455
@@ -495,7 +507,7 @@ private static Dictionary<Type, MethodInfo> InitializeReadMethods()
495507 [ typeof ( long ) ] = readerType . GetMethod ( nameof ( XmlReader . ReadElementContentAsLong ) , new [ ] { typeof ( string ) , typeof ( string ) } ) ,
496508 [ typeof ( float ) ] = readerType . GetMethod ( nameof ( XmlReader . ReadElementContentAsFloat ) , new [ ] { typeof ( string ) , typeof ( string ) } ) ,
497509 [ typeof ( double ) ] = readerType . GetMethod ( nameof ( XmlReader . ReadElementContentAsDouble ) , new [ ] { typeof ( string ) , typeof ( string ) } ) ,
498- [ typeof ( string ) ] = readerType . GetMethod ( nameof ( XmlReader . ReadElementContentAsString ) , new [ ] { typeof ( string ) , typeof ( string ) } ) ,
510+ [ typeof ( string ) ] = readerExtensionsType . GetMethod ( nameof ( XmlReaderExtensions . ReadElementAsNullableString ) ) ,
499511
500512 // Extensions
501513 [ typeof ( bool [ ] ) ] = readerExtensionsType . GetMethod ( nameof ( XmlReaderExtensions . ReadElementContentAsBooleanArray ) ) ,
0 commit comments