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

Fix S3257 FP: Following the suggestion to remove the explicit delegate creation, leads to runtime crash #9694

Open
lsw-sma opened this issue Nov 12, 2024 · 0 comments

Comments

@lsw-sma
Copy link

lsw-sma commented Nov 12, 2024

Description

Rule ID: S3257
UserControl provides the method AddHandler(RoutedEvent routedEvent, Delegate handler).
If I pass new RoutedEventHandler(OnErrorEvent) as the handler argument the rule is raised.
If I follow the suggestion and pass OnErrorEvent directly, it leads to the runtime exception: System.ArgumentException: 'Handler type is mismatched.'

Repro steps

  1. Create a System.Windows.Controls.UserControl (named UserControl1)
  2. Change the codebehind to the following:
public partial class UserControl1 {
    public UserControl1() {
        InitializeComponent();

        // Raises rule, no problem at runtime
        AddHandler(System.Windows.Controls.Validation.ErrorEvent, new RoutedEventHandler(OnErrorEvent));

        // Valid according to rule, but leads to runtime exception
        AddHandler(System.Windows.Controls.Validation.ErrorEvent, OnErrorEvent);
    }

    private void OnErrorEvent(object sender, RoutedEventArgs e) { }
}

Expected behavior

S3257 should not raise for passing new RoutedEventHandler(OnErrorEvent) to AddHandler.

Actual behavior

S3257 falsely suggests to pass the argument as a method group. This leads to a runtime exception: System.ArgumentException: 'Handler type is mismatched.'

Known workarounds

  1. Disable the rule locally.
  2. Extracting the argument as a variable, i.e.
var eventHandler = new RoutedEventHandler(OnErrorEvent);
AddHandler(System.Windows.Controls.Validation.ErrorEvent, eventHandler);

Related information

  • C#/VB.NET Plugins version: 8.0.400
  • Visual Studio version: VS 2022
  • MSBuild / dotnet version: 17.11.9+a69bbaaf5 for .NET Framework
  • Operating System: Win 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant