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

[Feature]: Custom ReferenceImporter in MemberCloner #283

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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public CloneContextAwareReferenceImporter(MemberCloneContext context)
_context = context;
}

/// <summary>
/// The working space for this member cloning procedure.
/// </summary>
protected MemberCloneContext Context => _context;

/// <inheritdoc />
protected override ITypeDefOrRef ImportType(TypeDefinition type)
{
Expand All @@ -41,4 +46,4 @@ public override IMethodDefOrRef ImportMethod(IMethodDefOrRef method)
: base.ImportMethod(method);
}
}
}
}
14 changes: 11 additions & 3 deletions src/AsmResolver.DotNet/Cloning/MemberCloneContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ public class MemberCloneContext
/// Creates a new instance of the <see cref="MemberCloneContext"/> class.
/// </summary>
/// <param name="module">The target module to copy the cloned members into.</param>
public MemberCloneContext(ModuleDefinition module)
public MemberCloneContext(ModuleDefinition module) : this(module, null) { }

/// <summary>
/// Creates a new instance of the <see cref="MemberCloneContext"/> class.
/// </summary>
/// <param name="module">The target module to copy the cloned members into.</param>
/// <param name="importerFactory">The factory for creating the reference importer</param>
public MemberCloneContext(ModuleDefinition module,
Func<MemberCloneContext, CloneContextAwareReferenceImporter>? importerFactory)
{
Module = module ?? throw new ArgumentNullException(nameof(module));
Importer = new CloneContextAwareReferenceImporter(this);
Importer = importerFactory?.Invoke(this) ?? new CloneContextAwareReferenceImporter(this);
}

/// <summary>
Expand All @@ -42,4 +50,4 @@ public ReferenceImporter Importer
get;
} = new Dictionary<IMemberDescriptor, IMemberDescriptor>();
}
}
}
14 changes: 12 additions & 2 deletions src/AsmResolver.DotNet/Cloning/MemberCloner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace AsmResolver.DotNet.Cloning
/// </remarks>
public partial class MemberCloner
{
private readonly Func<MemberCloneContext, CloneContextAwareReferenceImporter>? _importerFactory;
private readonly ModuleDefinition _targetModule;

private readonly HashSet<TypeDefinition> _typesToClone = new();
Expand All @@ -29,9 +30,18 @@ public partial class MemberCloner
/// Creates a new instance of the <see cref="MemberCloner"/> class.
/// </summary>
/// <param name="targetModule">The target module to copy the members into.</param>
public MemberCloner(ModuleDefinition targetModule)
public MemberCloner(ModuleDefinition targetModule) : this(targetModule, null) { }

/// <summary>
/// Creates a new instance of the <see cref="MemberCloner"/> class.
/// </summary>
/// <param name="targetModule">The target module to copy the members into.</param>
/// <param name="importerFactory">The factory for creating the reference importer</param>
public MemberCloner(ModuleDefinition targetModule,
Func<MemberCloneContext, CloneContextAwareReferenceImporter>? importerFactory)
{
_targetModule = targetModule ?? throw new ArgumentNullException(nameof(targetModule));
_importerFactory = importerFactory;
}

/// <summary>
Expand Down Expand Up @@ -220,7 +230,7 @@ public MemberCloner Include(EventDefinition @event)
/// <returns>An object representing the result of the cloning process.</returns>
public MemberCloneResult Clone()
{
var context = new MemberCloneContext(_targetModule);
var context = new MemberCloneContext(_targetModule, _importerFactory);

CreateMemberStubs(context);
DeepCopyMembers(context);
Expand Down