-
Notifications
You must be signed in to change notification settings - Fork 583
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
Improve Importer TryToUseTypeDefs #513
Conversation
@CreateAndInject I think I came up with a better way to implement this. Instead of ITypeDefOrRef CreateTypeDefOrRef(Type type) {
var tdr = mapper?.Map(type);
if (tdr is TypeSpec)
throw new InvalidOperationException();
if (tdr is TypeDef td)
return td;
if (tdr is TypeRef tr)
return TryResolve(tr);
if (TryToUseTypeDefs && IsThisModule(type.Module) && module.ResolveToken(type.MetadataToken) is TypeDef td2)
return td2;
return TryResolve(CreateTypeRef2(type));
} and also modify /// <summary>
/// Overrides default behavior of <see cref="Importer.Import(Type)"/>
/// May be used to use reference assemblies for <see cref="Type"/> resolution, for example.
/// </summary>
/// <param name="source"><see cref="Type"/> to create <see cref="ITypeDefOrRef"/> for. <paramref name="source"/> is non-generic type or generic type without generic arguments.</param>
/// <returns><see cref="ITypeDefOrRef"/> or null to use default <see cref="Importer"/>'s type resolution</returns>
public virtual ITypeDefOrRef Map(Type source) => null; What do you think? |
I considered
|
Remapping to a TypeDef can be beneficial for
If the user is customizing the import procedure, they should be familiar with how type and member references work and where they are or are not applicable. If we decide not to change ITypeDefOrRef CreateTypeDefOrRef(Type type) {
var tr = mapper?.Map(type);
if (tr is not null)
return TryResolve(tr);
if (TryToUseTypeDefs && IsThisModule(type.Module) && module.ResolveToken(type.MetadataToken) is TypeDef td)
return td;
return TryResolve(CreateTypeRef2(type));
} Using such an implementation over the current changes you implemented makes the code flow much clearer to the reader of the source code. |
Don't know if |
@CreateAndInject I don't use ImportMapper at all so let's see what people who actually use it think. |
22ea63a
to
6014534
Compare
I send a force-pushed to make the changes clear. |
Thanks! |
This PR resolve following issues:
Fix inconsistent type like this which for method/field:
Improve Importer TryToUseMethodDefs and TryToUseFieldDefs options 2 #443 8ed982f
TryToUseTypeDefs
can work also without creating anAssemblyResolver
(NullResolver
by default)