Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'Importer.Import(FieldInfo)' and 'Importer.ImportDeclaringType(Type)' #452

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions src/DotNet/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public Importer(ModuleDef module, ImporterOptions options, GenericParamContext g
/// </summary>
/// <param name="type">The type</param>
/// <returns></returns>
public ITypeDefOrRef ImportDeclaringType(Type type) => module.UpdateRowId(ImportAsTypeSig(type, type.IsGenericTypeDefinition).ToTypeDefOrRef());
public ITypeDefOrRef ImportDeclaringType(Type type) => module.UpdateRowId(ImportAsTypeSig(type, type.IsGenericButNotGenericTypeDefinition()).ToTypeDefOrRef());

/// <summary>
/// Imports a <see cref="Type"/> as a <see cref="ITypeDefOrRef"/>
Expand Down Expand Up @@ -621,26 +621,10 @@ public IField Import(FieldInfo fieldInfo, bool forceFixSignature) {
origField = fieldInfo;
}

MemberRef fieldRef;
if (origField.FieldType.ContainsGenericParameters) {
var origDeclType = origField.DeclaringType;
var asm = module.Context.AssemblyResolver.Resolve(origDeclType.Module.Assembly.GetName(), module);
if (asm is null || asm.FullName != origDeclType.Assembly.FullName)
throw new Exception("Couldn't resolve the correct assembly");
var mod = asm.FindModule(origDeclType.Module.ScopeName) as ModuleDefMD;
if (mod is null)
throw new Exception("Couldn't resolve the correct module");
var fieldDef = mod.ResolveField((uint)(origField.MetadataToken & 0x00FFFFFF));
if (fieldDef is null)
throw new Exception("Couldn't resolve the correct field");

var fieldSig = new FieldSig(Import(fieldDef.FieldSig.GetFieldType()));
fieldRef = module.UpdateRowId(new MemberRefUser(module, fieldInfo.Name, fieldSig, parent));
}
else {
var fieldSig = new FieldSig(ImportAsTypeSig(fieldInfo.FieldType, fieldInfo.GetRequiredCustomModifiers(), fieldInfo.GetOptionalCustomModifiers()));
fieldRef = module.UpdateRowId(new MemberRefUser(module, fieldInfo.Name, fieldSig, parent));
}
bool treatAsGenericInst = fieldInfo.DeclaringType.MustTreatTypeAsGenericInstType(origField.FieldType);
var fieldSig = new FieldSig(ImportAsTypeSig(origField.FieldType, origField.GetRequiredCustomModifiers(), origField.GetOptionalCustomModifiers(), treatAsGenericInst));
var fieldRef = module.UpdateRowId(new MemberRefUser(module, fieldInfo.Name, fieldSig, parent));

var field = TryResolveField(fieldRef);
if (FixSignature && !forceFixSignature) {
//TODO:
Expand Down
12 changes: 10 additions & 2 deletions src/DotNet/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public static ElementType GetElementType2(this Type a) {
return a.IsValueType ? ElementType.ValueType : ElementType.Class;
}

/// <summary>
/// Returns <c>true</c> if <paramref name="type"/> is a generic type, but
/// not a generic type definition, i.e., a TypeSpec.
/// </summary>
/// <param name="type">The type</param>
public static bool IsGenericButNotGenericTypeDefinition(this Type type) =>
type is not null && !type.IsGenericTypeDefinition && type.IsGenericType;

/// <summary>
/// Returns <c>true</c> if <paramref name="mb"/> is a generic method, but
/// not a generic method definition, i.e., a MethodSpec.
Expand All @@ -94,8 +102,8 @@ public static bool IsGenericButNotGenericMethodDefinition(this MethodBase mb) =>
/// a generic type def. This seems to happen only if the parameter type is exactly the same
/// type as the declaring type, eg. a method similar to: <c>MyType&lt;!0&gt; MyType::SomeMethod()</c>.
/// </summary>
/// <param name="declaringType">Declaring type of method/event/property</param>
/// <param name="t">Parameter/property/event type</param>
/// <param name="declaringType">Declaring type of field/method/event/property</param>
/// <param name="t">Field/parameter/property/event type</param>
internal static bool MustTreatTypeAsGenericInstType(this Type declaringType, Type t) =>
declaringType is not null && declaringType.IsGenericTypeDefinition && t == declaringType;

Expand Down