@@ -350,18 +350,20 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
350
350
if ( mr != null &&
351
351
mr . ReturnType != null )
352
352
{
353
- parameters = mr . Parameters ;
354
-
355
353
if ( mr . DeclaringType != null )
356
354
{
357
355
if ( mr . DeclaringType is TypeSpecification )
358
356
{
359
357
// 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
360
358
if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( mr . DeclaringType , out ushort referenceId ) )
361
359
{
360
+ // add "fabricated" token for TypeSpec using the referenceId as RID
362
361
set . Add ( new MetadataToken (
363
362
TokenType . TypeSpec ,
364
363
referenceId ) ) ;
364
+
365
+ // add the metadata token for the element type
366
+ set . Add ( mr . DeclaringType . GetElementType ( ) . MetadataToken ) ;
365
367
}
366
368
else
367
369
{
@@ -460,9 +462,13 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
460
462
// 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
461
463
if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( fr . DeclaringType , out ushort referenceId ) )
462
464
{
465
+ // add "fabricated" token for TypeSpec using the referenceId as RID
463
466
set . Add ( new MetadataToken (
464
467
TokenType . TypeSpec ,
465
468
referenceId ) ) ;
469
+
470
+ // add the metadata token for the element type
471
+ set . Add ( fr . DeclaringType . GetElementType ( ) . MetadataToken ) ;
466
472
}
467
473
else
468
474
{
@@ -518,14 +524,33 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
518
524
break ;
519
525
520
526
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 )
524
539
{
540
+ // found it, let's add it
525
541
set . Add ( token ) ;
526
542
}
527
543
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 ) ;
529
554
530
555
break ;
531
556
@@ -807,10 +832,12 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
807
832
// 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
808
833
if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( v . VariableType , out ushort referenceId ) )
809
834
{
835
+ // add "fabricated" token for TypeSpec using the referenceId as RID
810
836
set . Add ( new MetadataToken (
811
837
TokenType . TypeSpec ,
812
838
referenceId ) ) ;
813
839
840
+ // add the metadata token for the element type
814
841
set . Add ( v . VariableType . GetElementType ( ) . MetadataToken ) ;
815
842
}
816
843
else
@@ -844,9 +871,13 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
844
871
// 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
845
872
if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( methodReferenceType . DeclaringType , out referenceId ) )
846
873
{
874
+ // add "fabricated" token for TypeSpec using the referenceId as RID
847
875
set . Add ( new MetadataToken (
848
876
TokenType . TypeSpec ,
849
877
referenceId ) ) ;
878
+
879
+ // add the metadata token for the element type
880
+ set . Add ( methodReferenceType . DeclaringType . GetElementType ( ) . MetadataToken ) ;
850
881
}
851
882
else
852
883
{
@@ -877,9 +908,13 @@ i.Operand is GenericInstanceMethod ||
877
908
// 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
878
909
if ( _tablesContext . TypeSpecificationsTable . TryGetTypeReferenceId ( opType , out ushort referenceId ) )
879
910
{
911
+ // add "fabricated" token for TypeSpec using the referenceId as RID
880
912
set . Add ( new MetadataToken (
881
913
TokenType . TypeSpec ,
882
914
referenceId ) ) ;
915
+
916
+ // add the metadata token for the element type
917
+ set . Add ( opType . GetElementType ( ) . MetadataToken ) ;
883
918
}
884
919
else
885
920
{
0 commit comments