Skip to content

Add trimming intrinsic for EventInfo.EventHandlerType.GetMethod("Invoke") - #116931

Merged
sbomer merged 2 commits into
dotnet:mainfrom
sbomer:eventInvokeMethod
Jun 24, 2025
Merged

Add trimming intrinsic for EventInfo.EventHandlerType.GetMethod("Invoke")#116931
sbomer merged 2 commits into
dotnet:mainfrom
sbomer:eventInvokeMethod

Conversation

@sbomer

Copy link
Copy Markdown
Member

Fixes#114113

Because ILLink and ILC keep the Invoke method on delegates, there shouldn't be a warning for the common pattern of EventInfo.EventHandlerType.GetMethod("Invoke").

It's possible to break this logic by using a custom EventInfo - see the testcase and discussion in the original issue.

@sbomer
sbomer requested review from a team and CopilotJune 23, 2025 19:38
@github-actionsgithub-actionsBot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Jun 23, 2025
@dotnet-policy-servicedotnet-policy-serviceBot added the linkable-framework Issues associated with delivering a linker friendly framework label Jun 23, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/illink
See info in area-owners.md if you want to be subscribed.

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR prevents spurious trimming warnings when calling EventInfo.EventHandlerType.GetMethod("Invoke") on delegates by adding special-case handling in the dataflow logic, and adds a test covering both delegate and custom-EventInfo scenarios.

  • Added a new test case EventHanderTypeGetInvokeMethod to cover the delegate and non-delegate paths.
  • Updated HandleCallAction to recognize and allow Invoke on delegate types without additional requirements.
  • Refactored several MethodReturnValue implementations to carry both the original method proxy and its definition.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
FileDescription
src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/EventHanderTypeGetInvokeMethod.csNew test exercising delegate and custom EventInfo Invoke scenarios
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/ReflectionTests.csRegistered new analyzer test for the above case
src/tools/illink/src/linker/Linker.Dataflow/MethodReturnValue.csSwitched to carrying a MethodProxy and a MethodDefinition
src/tools/illink/src/ILLink.Shared/TrimAnalysis/MethodReturnValue.csAdded Method property to shared trim analysis value
src/tools/illink/src/ILLink.Shared/TrimAnalysis/HandleCallAction.csSpecial-case logic for GetMethod("Invoke") on delegates
src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/MethodReturnValue.csUpdated Roslyn analyzer to construct MethodProxy
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/MethodReturnValue.csUpdated AOT dataflow to construct MethodProxy and separate MethodDesc
Comments suppressed due to low confidence (5)

src/tools/illink/src/linker/Linker.Dataflow/MethodReturnValue.cs:34

  • The constructor assigns to Method but no Method field or property is declared in this class. Add a public readonly MethodProxy Method; (or auto-property) to match the assignment and avoid a compile error.
 Method = method;

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/MethodReturnValue.cs:25

  • This line assigns to Method but the class does not declare a Method field or property. Introduce a public MethodProxy Method { get; } to store the proxy and ensure the code compiles.
 Method = new MethodProxy (methodSymbol);

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Dataflow/MethodReturnValue.cs:26

  • The constructor sets Method but no such member exists. Add a public MethodProxy Method { get; } or similar declaration so the code compiles and can supply the proxy where needed.
 Method = new MethodProxy(method);

src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/EventHanderTypeGetInvokeMethod.cs:14

  • [nitpick] The class name EventHanderTypeGetInvokeMethod is missing an 'l' in Handler. Consider renaming to EventHandlerTypeGetInvokeMethod for clarity and consistency.
	public class EventHanderTypeGetInvokeMethod

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/ReflectionTests.cs:38

  • [nitpick] The test method name EventHanderTypeGetInvokeMethod is missing an 'l' in Handler. Update it to EventHandlerTypeGetInvokeMethod to match the corrected class name.
 public Task EventHanderTypeGetInvokeMethod ()

@sbomersbomer changed the title Event invoke methodAdd trimming intrinsic for EventInfo.EventHandlerType.GetMethod("Invoke")Jun 23, 2025

@jtschusterjtschuster left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@sbomer
sbomer merged commit 102c597 into dotnet:mainJun 24, 2025
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Jul 25, 2025
@sbomer
sbomer deleted the eventInvokeMethod branch June 26, 2026 18:07
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzerslinkable-frameworkIssues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EventInfo.EventHandlerType.GetMethod("Invoke") could be made trim safe

4 participants

@sbomer@MichalStrehovsky@jtschuster