@@ -350,18 +350,20 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
350350 if ( mr != null &&
351351 mr . ReturnType != null )
352352 {
353- parameters = mr . Parameters ;
354-
355353 if ( mr . DeclaringType != null )
356354 {
357355 if ( mr . DeclaringType is TypeSpecification )
358356 {
359357 // Cecil.Mono has a bug providing TypeSpecs Metadata tokens generic parameters variables, so we need to check against our internal table and build one from it
360358 if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( mr . DeclaringType , out ushort referenceId ) )
361359 {
360+ // add "fabricated" token for TypeSpec using the referenceId as RID
362361 set . Add ( new MetadataToken (
363362 TokenType . TypeSpec ,
364363 referenceId ) ) ;
364+
365+ // add the metadata token for the element type
366+ set . Add ( mr . DeclaringType . GetElementType ( ) . MetadataToken ) ;
365367 }
366368 else
367369 {
@@ -460,9 +462,13 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
460462 // Cecil.Mono has a bug providing TypeSpecs Metadata tokens generic parameters variables, so we need to check against our internal table and build one from it
461463 if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( fr . DeclaringType , out ushort referenceId ) )
462464 {
465+ // add "fabricated" token for TypeSpec using the referenceId as RID
463466 set . Add ( new MetadataToken (
464467 TokenType . TypeSpec ,
465468 referenceId ) ) ;
469+
470+ // add the metadata token for the element type
471+ set . Add ( fr . DeclaringType . GetElementType ( ) . MetadataToken ) ;
466472 }
467473 else
468474 {
@@ -518,14 +524,33 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
518524 break ;
519525
520526 case TokenType . TypeSpec :
521- var ts = _tablesContext . TypeSpecificationsTable . TryGetTypeSpecification ( token ) ;
522-
523- if ( ts != null )
527+ // Developer notes:
528+ // Because the issue with Mono.Cecil not providing the correct metadata token for TypeSpec,
529+ // need to search the TypeSpec token in the two "formats".
530+ // 1. The original token as provided by Mono.Cecil
531+ // 2. The "fabricated" token, which is the one that is used in the TypeSpec table
532+ // It's OK to add both to the set because they end up referencing to the same type.
533+ // Anyways, as this a minimize operation, it's preferable to have both rather than none.
534+
535+ // start searching for metadata token
536+ TypeReference ts1 = _tablesContext . TypeSpecificationsTable . TryGetTypeSpecification ( token ) ;
537+
538+ if ( ts1 != null )
524539 {
540+ // found it, let's add it
525541 set . Add ( token ) ;
526542 }
527543
528- Debug . Assert ( ts != null ) ;
544+ // now try to find the TypeSpec from the "fabricated" token, using the RID
545+ TypeReference ts2 = _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceByIndex ( ( ushort ) token . RID ) ;
546+
547+ if ( ts2 != null )
548+ {
549+ set . Add ( ts2 . MetadataToken ) ;
550+ }
551+
552+ // sanity check
553+ Debug . Assert ( ts1 != null || ts2 != null ) ;
529554
530555 break ;
531556
@@ -807,10 +832,12 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
807832 // Cecil.Mono has a bug providing TypeSpecs Metadata tokens generic parameters variables, so we need to check against our internal table and build one from it
808833 if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( v . VariableType , out ushort referenceId ) )
809834 {
835+ // add "fabricated" token for TypeSpec using the referenceId as RID
810836 set . Add ( new MetadataToken (
811837 TokenType . TypeSpec ,
812838 referenceId ) ) ;
813839
840+ // add the metadata token for the element type
814841 set . Add ( v . VariableType . GetElementType ( ) . MetadataToken ) ;
815842 }
816843 else
@@ -844,9 +871,13 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
844871 // Cecil.Mono has a bug providing TypeSpecs Metadata tokens generic parameters variables, so we need to check against our internal table and build one from it
845872 if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( methodReferenceType . DeclaringType , out referenceId ) )
846873 {
874+ // add "fabricated" token for TypeSpec using the referenceId as RID
847875 set . Add ( new MetadataToken (
848876 TokenType . TypeSpec ,
849877 referenceId ) ) ;
878+
879+ // add the metadata token for the element type
880+ set . Add ( methodReferenceType . DeclaringType . GetElementType ( ) . MetadataToken ) ;
850881 }
851882 else
852883 {
@@ -877,9 +908,13 @@ i.Operand is GenericInstanceMethod ||
877908 // Cecil.Mono has a bug providing TypeSpecs Metadata tokens generic parameters variables, so we need to check against our internal table and build one from it
878909 if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( opType , out ushort referenceId ) )
879910 {
911+ // add "fabricated" token for TypeSpec using the referenceId as RID
880912 set . Add ( new MetadataToken (
881913 TokenType . TypeSpec ,
882914 referenceId ) ) ;
915+
916+ // add the metadata token for the element type
917+ set . Add ( opType . GetElementType ( ) . MetadataToken ) ;
883918 }
884919 else
885920 {
0 commit comments