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 OnErrorEventdirectly, it leads to the runtime exception: System.ArgumentException: 'Handler type is mismatched.'
Repro steps
Create a System.Windows.Controls.UserControl (named UserControl1)
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
Disable the rule locally.
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
Description
Rule ID: S3257
UserControl
provides the methodAddHandler(RoutedEvent routedEvent, Delegate handler)
. If I passnew RoutedEventHandler(OnErrorEvent)
as thehandler
argument the rule is raised. If I follow the suggestion and passOnErrorEvent
directly, it leads to the runtime exception:System.ArgumentException: 'Handler type is mismatched.'
Repro steps
System.Windows.Controls.UserControl
(named UserControl1)Change the codebehind to the following:
Expected behavior
S3257 should not raise for passing
new RoutedEventHandler(OnErrorEvent)
toAddHandler
.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
Related information