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

Inconsistent MemberCloner behavior when cloning properties and events #591

Closed
gotmachine opened this issue Oct 29, 2024 · 2 comments
Closed
Labels
bug dotnet Issues related to AsmResolver.DotNet
Milestone

Comments

@gotmachine
Copy link

gotmachine commented Oct 29, 2024

AsmResolver Version

5.5.1.0

.NET Version

.net standard 2.0

Operating System

Windows

Describe the Bug

When cloning individual properties or events with MemberCloner.Include(PropertyDefinition) or MemberCloner.Include(EventDefinition), the cloned members aren't added to the resulting MemberCloneResult.

Additionally, and contrary to what the documentation says :

It is important to note that the MemberCloner class itself does not inject any of the cloned members by itself.

Cloned properties and events are actually injected in their DeclaringType, but only if that type was explicitly cloned too using MemberCloner.Include(TypeDefinition) (or one of its variants) :

// If property's declaring type is cloned as well, add the cloned property to the cloned type.
if (property.DeclaringType is not null
&& context.ClonedMembers.TryGetValue(property.DeclaringType, out var member)
&& member is TypeDefinition declaringType)
{
declaringType.Properties.Add(clonedProperty);
}

// If event's declaring type is cloned as well, add the cloned event to the cloned type.
if (@event.DeclaringType is not null
&& context.ClonedMembers.TryGetValue(@event.DeclaringType, out var member)
&& member is TypeDefinition declaringType)
{
declaringType.Events.Add(clonedEvent);
}

This behavior is inconsistent with what happens when cloning fields or methods, which are accessible in MemberCloneResult, and not automatically added to their declaring type.

When the declaring type isn't cloned, the only workaround to get back a reference to those cloned members is to register a custom IMemberClonerListener to the MemberCloner instance, implementing the OnClonedProperty(PropertyDefinition original, PropertyDefinition cloned) or OnClonedEvent(EventDefinition original, EventDefinition cloned) methods.

How To Reproduce

  • Clone individual properties or events with MemberCloner.Include()
  • Call MemberCloner.Clone()

Expected Behavior

  • The resulting MemberCloneResult has references to the original and cloned properties or events.
  • The cloned properties or events aren't automatically added to their declaring type when that type was also explicitly cloned.

Actual Behavior

  • The resulting MemberCloneResult doesn't have any reference to either the original or cloned properties or events.
  • The cloned properties or events are automatically added to their declaring type when that type was also explicitly cloned.
@gotmachine gotmachine added the bug label Oct 29, 2024
@Washi1337 Washi1337 added the dotnet Issues related to AsmResolver.DotNet label Oct 29, 2024
@Washi1337 Washi1337 added this to the 6.0.0 milestone Oct 29, 2024
@Washi1337
Copy link
Owner

Washi1337 commented Nov 3, 2024

Potential fix is in #592, if you can verify this fixes your issues, that would be great.

Check GitHub Actions for staging build artifacts.

@Washi1337
Copy link
Owner

Assuming this has fixed the issue, I will close this issue.

Feel free to reopen/comment on the issue if problems persist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug dotnet Issues related to AsmResolver.DotNet
Projects
None yet
Development

No branches or pull requests

2 participants