Skip to content

Commit

Permalink
Improve Importer TryToUseTypeDefs
Browse files Browse the repository at this point in the history
  • Loading branch information
CreateAndInject committed Jul 11, 2023
1 parent 2afeaee commit 22ea63a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/DotNet/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,15 @@ TypeSig ImportAsTypeSig(Type type, Type declaringType, bool? treatAsGenericInst
}
}

ITypeDefOrRef TryResolve(TypeRef tr) {
ITypeDefOrRef TryResolve(ITypeDefOrRef type) {
if (type is TypeDef td)
return td;
var tr = type as TypeRef;
if (!TryToUseTypeDefs || tr is null)
return tr;
if (!IsThisModule(tr))
return tr;
var td = tr.Resolve();
td = tr.Resolve();
if (td is null || td.Module != module)
return tr;
return td;
Expand Down Expand Up @@ -358,11 +361,13 @@ static bool Equals(IAssembly a, IAssembly b) {

ITypeDefOrRef CreateTypeRef(Type type) => TryResolve(mapper?.Map(type) ?? CreateTypeRef2(type));

TypeRef CreateTypeRef2(Type type) {
ITypeDefOrRef CreateTypeRef2(Type type) {
if (TryToUseTypeDefs && IsThisModule(type.Module) && module.ResolveToken(type.MetadataToken) is TypeDef td)
return td;
if (!type.IsNested)
return module.UpdateRowId(new TypeRefUser(module, type.Namespace ?? string.Empty, ReflectionExtensions.Unescape(type.Name) ?? string.Empty, CreateScopeReference(type)));
type.GetTypeNamespaceAndName_TypeDefOrRef(out var @namespace, out var name);
return module.UpdateRowId(new TypeRefUser(module, @namespace ?? string.Empty, name ?? string.Empty, CreateTypeRef2(type.DeclaringType)));
return module.UpdateRowId(new TypeRefUser(module, @namespace ?? string.Empty, name ?? string.Empty, (TypeRef)CreateTypeRef2(type.DeclaringType)));
}

IResolutionScope CreateScopeReference(Type type) {
Expand Down

0 comments on commit 22ea63a

Please sign in to comment.