3
3
using System ;
4
4
using System . Collections . Generic ;
5
5
using System . Reflection ;
6
+ using dnlib . DotNet . Emit ;
6
7
7
8
namespace dnlib . DotNet {
8
9
/// <summary>
@@ -167,22 +168,22 @@ public Importer(ModuleDef module, ImporterOptions options, GenericParamContext g
167
168
}
168
169
169
170
/// <summary>
170
- /// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>.
171
+ /// Imports a <see cref="Type"/> as an <see cref="ITypeDefOrRef"/>.
171
172
/// </summary>
172
173
/// <param name="type">The type</param>
173
174
/// <returns>The imported type or <c>null</c> if <paramref name="type"/> is invalid</returns>
174
175
public ITypeDefOrRef Import ( Type type ) => module . UpdateRowId ( ImportAsTypeSig ( type ) . ToTypeDefOrRef ( ) ) ;
175
176
176
177
/// <summary>
177
- /// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>. See also <see cref="Import(Type)"/>
178
+ /// Imports a <see cref="Type"/> as an <see cref="ITypeDefOrRef"/>. See also <see cref="Import(Type)"/>
178
179
/// </summary>
179
180
/// <param name="type">The type</param>
180
181
/// <returns></returns>
181
182
[ Obsolete ( "Use 'Import(Type)' instead." ) ]
182
183
public ITypeDefOrRef ImportDeclaringType ( Type type ) => Import ( type ) ;
183
184
184
185
/// <summary>
185
- /// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>
186
+ /// Imports a <see cref="Type"/> as an <see cref="ITypeDefOrRef"/>
186
187
/// </summary>
187
188
/// <param name="type">The type</param>
188
189
/// <param name="requiredModifiers">A list of all required modifiers or <c>null</c></param>
@@ -399,7 +400,7 @@ IResolutionScope CreateScopeReference(Type type) {
399
400
}
400
401
401
402
/// <summary>
402
- /// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>
403
+ /// Imports a <see cref="Type"/> as an <see cref="ITypeDefOrRef"/>
403
404
/// </summary>
404
405
/// <param name="type">The type</param>
405
406
/// <param name="requiredModifiers">A list of all required modifiers or <c>null</c></param>
@@ -437,17 +438,15 @@ TypeSig ImportAsTypeSig(Type type, IList<Type> requiredModifiers, IList<Type> op
437
438
static bool IsEmpty < T > ( IList < T > list ) => list is null || list . Count == 0 ;
438
439
439
440
/// <summary>
440
- /// Imports a <see cref="MethodBase"/> as a <see cref="IMethod"/>. This will be either
441
- /// a <see cref="MemberRef"/> or a <see cref="MethodSpec"/>.
441
+ /// Imports a <see cref="MethodBase"/> as an <see cref="IMethod"/>.
442
442
/// </summary>
443
443
/// <param name="methodBase">The method</param>
444
444
/// <returns>The imported method or <c>null</c> if <paramref name="methodBase"/> is invalid
445
445
/// or if we failed to import the method</returns>
446
446
public IMethod Import ( MethodBase methodBase ) => Import ( methodBase , false ) ;
447
447
448
448
/// <summary>
449
- /// Imports a <see cref="MethodBase"/> as a <see cref="IMethod"/>. This will be either
450
- /// a <see cref="MemberRef"/> or a <see cref="MethodSpec"/>.
449
+ /// Imports a <see cref="MethodBase"/> as an <see cref="IMethod"/>.
451
450
/// </summary>
452
451
/// <param name="methodBase">The method</param>
453
452
/// <param name="forceFixSignature">Always verify method signature to make sure the
@@ -459,9 +458,7 @@ public IMethod Import(MethodBase methodBase, bool forceFixSignature) {
459
458
return ImportInternal ( methodBase , forceFixSignature ) ;
460
459
}
461
460
462
- IMethod ImportInternal ( MethodBase methodBase ) => ImportInternal ( methodBase , false ) ;
463
-
464
- IMethod ImportInternal ( MethodBase methodBase , bool forceFixSignature ) {
461
+ IMethod ImportInternal ( MethodBase methodBase , bool forceFixSignature , bool asOperand = false ) {
465
462
if ( methodBase is null )
466
463
return null ;
467
464
@@ -484,13 +481,13 @@ IMethod ImportInternal(MethodBase methodBase, bool forceFixSignature) {
484
481
if ( methodBase . DeclaringType . GetElementType2 ( ) == ElementType . GenericInst )
485
482
method = module . UpdateRowId ( new MemberRefUser ( module , methodBase . Name , CreateMethodSig ( origMethod ) , Import ( methodBase . DeclaringType ) ) ) ;
486
483
else
487
- method = ImportInternal ( origMethod ) as IMethodDefOrRef ;
484
+ method = ImportInternal ( origMethod , forceFixSignature , asOperand ) as IMethodDefOrRef ;
488
485
489
486
method = TryResolveMethod ( method ) ;
490
- if ( methodBase . ContainsGenericParameters )
487
+ if ( ! asOperand && methodBase . ContainsGenericParameters )
491
488
return method ; // Declaring type is instantiated but method itself is not
492
489
493
- var gim = CreateGenericInstMethodSig ( methodBase ) ;
490
+ var gim = CreateGenericInstMethodSig ( methodBase , asOperand ) ;
494
491
var methodSpec = module . UpdateRowId ( new MethodSpecUser ( method , gim ) ) ;
495
492
if ( FixSignature && ! forceFixSignature ) {
496
493
//TODO:
@@ -504,7 +501,7 @@ IMethod ImportInternal(MethodBase methodBase, bool forceFixSignature) {
504
501
parent = GetModuleParent ( methodBase . Module ) ;
505
502
}
506
503
else
507
- parent = Import ( methodBase . DeclaringType ) ;
504
+ parent = asOperand ? ImportAsOperand ( methodBase . DeclaringType ) : Import ( methodBase . DeclaringType ) ;
508
505
if ( parent is null )
509
506
return null ;
510
507
@@ -583,11 +580,11 @@ CallingConvention GetCallingConvention(MethodBase mb) {
583
580
return cc ;
584
581
}
585
582
586
- GenericInstMethodSig CreateGenericInstMethodSig ( MethodBase mb ) {
583
+ GenericInstMethodSig CreateGenericInstMethodSig ( MethodBase mb , bool asOperand ) {
587
584
var genMethodArgs = mb . GetGenericArguments ( ) ;
588
585
var gim = new GenericInstMethodSig ( CallingConvention . GenericInst , ( uint ) genMethodArgs . Length ) ;
589
586
foreach ( var gma in genMethodArgs )
590
- gim . GenericArguments . Add ( ImportAsTypeSig ( gma ) ) ;
587
+ gim . GenericArguments . Add ( asOperand ? ImportAsTypeSig ( gma , null , gma . IsGenericType ) : ImportAsTypeSig ( gma ) ) ;
591
588
return gim ;
592
589
}
593
590
@@ -604,22 +601,24 @@ bool IsThisAssembly(Module module2) {
604
601
}
605
602
606
603
/// <summary>
607
- /// Imports a <see cref="FieldInfo"/> as a <see cref="MemberRef "/>
604
+ /// Imports a <see cref="FieldInfo"/> as an <see cref="IField "/>
608
605
/// </summary>
609
606
/// <param name="fieldInfo">The field</param>
610
607
/// <returns>The imported field or <c>null</c> if <paramref name="fieldInfo"/> is invalid
611
608
/// or if we failed to import the field</returns>
612
609
public IField Import ( FieldInfo fieldInfo ) => Import ( fieldInfo , false ) ;
613
610
614
611
/// <summary>
615
- /// Imports a <see cref="FieldInfo"/> as a <see cref="MemberRef "/>
612
+ /// Imports a <see cref="FieldInfo"/> as an <see cref="IField "/>
616
613
/// </summary>
617
614
/// <param name="fieldInfo">The field</param>
618
615
/// <param name="forceFixSignature">Always verify field signature to make sure the
619
616
/// returned reference matches the metadata in the source assembly</param>
620
617
/// <returns>The imported field or <c>null</c> if <paramref name="fieldInfo"/> is invalid
621
618
/// or if we failed to import the field</returns>
622
- public IField Import ( FieldInfo fieldInfo , bool forceFixSignature ) {
619
+ public IField Import ( FieldInfo fieldInfo , bool forceFixSignature ) => ImportInternal ( fieldInfo , forceFixSignature , false ) ;
620
+
621
+ IField ImportInternal ( FieldInfo fieldInfo , bool forceFixSignature , bool asOperand = false ) {
623
622
FixSignature = false ;
624
623
if ( fieldInfo is null )
625
624
return null ;
@@ -642,7 +641,7 @@ public IField Import(FieldInfo fieldInfo, bool forceFixSignature) {
642
641
parent = GetModuleParent ( fieldInfo . Module ) ;
643
642
}
644
643
else
645
- parent = Import ( fieldInfo . DeclaringType ) ;
644
+ parent = asOperand ? ImportAsOperand ( fieldInfo . DeclaringType ) : Import ( fieldInfo . DeclaringType ) ;
646
645
if ( parent is null )
647
646
return null ;
648
647
@@ -656,7 +655,7 @@ public IField Import(FieldInfo fieldInfo, bool forceFixSignature) {
656
655
origField = fieldInfo ;
657
656
}
658
657
659
- var fieldSig = new FieldSig ( ImportAsTypeSig ( origField . FieldType ,
658
+ var fieldSig = new FieldSig ( ImportAsTypeSig ( origField . FieldType ,
660
659
origField . GetRequiredCustomModifiers ( ) , origField . GetOptionalCustomModifiers ( ) , origField . DeclaringType ) ) ;
661
660
var fieldRef = module . UpdateRowId ( new MemberRefUser ( module , fieldInfo . Name , fieldSig , parent ) ) ;
662
661
@@ -1190,5 +1189,28 @@ IMemberRefParent Import(IMemberRefParent parent) {
1190
1189
1191
1190
return null ;
1192
1191
}
1192
+
1193
+ /// <summary>
1194
+ /// Imports a <see cref="Type"/> as an <see cref="ITypeDefOrRef"/> used for <see cref="Instruction.Operand"/>.
1195
+ /// </summary>
1196
+ /// <param name="type">The type</param>
1197
+ /// <returns>The imported type or <c>null</c> if <paramref name="type"/> is invalid</returns>
1198
+ public ITypeDefOrRef ImportAsOperand ( Type type ) => module . UpdateRowId ( ImportAsTypeSig ( type , null , type . IsGenericType ) . ToTypeDefOrRef ( ) ) ;
1199
+
1200
+ /// <summary>
1201
+ /// Imports a <see cref="MethodBase"/> as an <see cref="IMethod"/> used for <see cref="Instruction.Operand"/>.
1202
+ /// </summary>
1203
+ /// <param name="methodBase">The method</param>
1204
+ /// <returns>The imported method or <c>null</c> if <paramref name="methodBase"/> is invalid
1205
+ /// or if we failed to import the method</returns>
1206
+ public IMethod ImportAsOperand ( MethodBase methodBase ) => ImportInternal ( methodBase , false , true ) ;
1207
+
1208
+ /// <summary>
1209
+ /// Imports a <see cref="FieldInfo"/> as an <see cref="IField"/> used for <see cref="Instruction.Operand"/>.
1210
+ /// </summary>
1211
+ /// <param name="fieldInfo">The field</param>
1212
+ /// <returns>The imported field or <c>null</c> if <paramref name="fieldInfo"/> is invalid
1213
+ /// or if we failed to import the field</returns>
1214
+ public IField ImportAsOperand ( FieldInfo fieldInfo ) => ImportInternal ( fieldInfo , false , true ) ;
1193
1215
}
1194
1216
}
0 commit comments