[.NET 7.0.1] InvalidCastException in ErrorProvider
See original GitHub issue.NET version
7.0.1
Did it work in .NET Framework?
Yes
Did it work in any of the earlier releases of .NET Core or .NET 5+?
No response
Issue description
System.InvalidCastException exception is thrown in ErrorProvider.
Steps to reproduce
- Create a .NET 7.0 WinForm app.
- Replace Form1.cs with the following code
- Launch the app.
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var model = new MyModel();
this.DataBindings.Add("Text", model, "Error");
new ErrorProvider(this).DataSource = model; // InvalidCastException !!!
}
}
public class MyModel : System.ComponentModel.IDataErrorInfo
{
public string this[string columnName] => "";
public string Error => "";
}
}
Excepted Result: No exception thrown.
Actual Result: This exception is thrown in code:
System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type ‘System.Collections.Generic.KeyValuePair`2[System.Windows.Forms.Control,System.String]’ to type ‘System.Collections.DictionaryEntry’. Source=System.Windows.Forms Stack Trace: at System.Windows.Forms.ErrorProvider.ErrorManager_CurrentChanged(Object sender, EventArgs e) at System.Windows.Forms.ErrorProvider.SetErrorManager(Object newDataSource, String newDataMember, Boolean force) at WinFormsApp1.Form1…ctor() (Form1.cs):line 13 at WinFormsApp1.Program.Main() (Program.cs):line 14
Other information https://github.com/dotnet/winforms/pull/7037
// Hashtable controlError = new Hashtable(bindingsCount);
Dictionary<Control, string> controlError = new(bindingsCount);
...
IEnumerator enumerator = controlError.GetEnumerator();
while (enumerator.MoveNext())
{
DictionaryEntry entry = (DictionaryEntry)enumerator.Current; // Invalid Cast !!!
SetError((Control)entry.Key, (string?)entry.Value);
}
Issue Analytics
- State:
- Created 9 months ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Sorry, I should have checked that part of the code when I changed it. I added a small PR with a fix.
Verified with .NET 8 Preview 1 TP build: .NET 8.0.100-preview.1.23108.10, issue was fixed as same as above test result.