Skip to content

Commit

Permalink
Fix Importer when importing FieldInfo/MethodInfo with generic owner, c…
Browse files Browse the repository at this point in the history
…loses 0xd4d#317
  • Loading branch information
wtfsck committed Mar 20, 2020
1 parent b97a464 commit d84c7cc
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/DotNet/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,21 @@ public Importer(ModuleDef module, ImporterOptions options, GenericParamContext g
}

/// <summary>
/// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>
/// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>. If it's a type that should be
/// the declaring type of a field/method reference, call <see cref="ImportDeclaringType(Type)"/> instead.
/// </summary>
/// <param name="type">The type</param>
/// <returns>The imported type or <c>null</c> if <paramref name="type"/> is invalid</returns>
public ITypeDefOrRef Import(Type type) => module.UpdateRowId(ImportAsTypeSig(type).ToTypeDefOrRef());

/// <summary>
/// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>. Should be called if it's the
/// declaring type of a method/field reference. See also <see cref="Import(Type)"/>
/// </summary>
/// <param name="type">The type</param>
/// <returns></returns>
public ITypeDefOrRef ImportDeclaringType(Type type) => module.UpdateRowId(ImportAsTypeSig(type, type.IsGenericTypeDefinition).ToTypeDefOrRef());

/// <summary>
/// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>
/// </summary>
Expand Down Expand Up @@ -444,7 +453,7 @@ IMethod ImportInternal(MethodBase methodBase, bool forceFixSignature) {
IMethodDefOrRef method;
var origMethod = methodBase.Module.ResolveMethod(methodBase.MetadataToken);
if (methodBase.DeclaringType.GetElementType2() == ElementType.GenericInst)
method = module.UpdateRowId(new MemberRefUser(module, methodBase.Name, CreateMethodSig(origMethod), Import(methodBase.DeclaringType)));
method = module.UpdateRowId(new MemberRefUser(module, methodBase.Name, CreateMethodSig(origMethod), ImportDeclaringType(methodBase.DeclaringType)));
else
method = ImportInternal(origMethod) as IMethodDefOrRef;

Expand All @@ -464,7 +473,7 @@ IMethod ImportInternal(MethodBase methodBase, bool forceFixSignature) {
parent = GetModuleParent(methodBase.Module);
}
else
parent = Import(methodBase.DeclaringType);
parent = ImportDeclaringType(methodBase.DeclaringType);
if (parent is null)
return null;

Expand Down Expand Up @@ -590,7 +599,7 @@ public IField Import(FieldInfo fieldInfo, bool forceFixSignature) {
parent = GetModuleParent(fieldInfo.Module);
}
else
parent = Import(fieldInfo.DeclaringType);
parent = ImportDeclaringType(fieldInfo.DeclaringType);
if (parent is null)
return null;

Expand Down

0 comments on commit d84c7cc

Please sign in to comment.