Washi1337 / AsmResolver

A library for creating, reading and editing PE files and .NET modules.
https://docs.washi.dev/asmresolver/
MIT License
862 stars 127 forks source link

Inconsistent `MemberCloner` behavior when cloning properties and events #591

Open gotmachine opened 1 week ago

gotmachine commented 1 week ago

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) : https://github.com/Washi1337/AsmResolver/blob/f70da7936c203b4ecfb19c9ad71d08c6eb4b5bd3/src/AsmResolver.DotNet/Cloning/MemberCloner.Semantics.cs#L13-L19 https://github.com/Washi1337/AsmResolver/blob/f70da7936c203b4ecfb19c9ad71d08c6eb4b5bd3/src/AsmResolver.DotNet/Cloning/MemberCloner.Semantics.cs#L50-L56

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

Expected Behavior

Actual Behavior

Washi1337 commented 4 days ago

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

Check GitHub Actions for staging build artifacts.