-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Description
The backing property XmlNamespaceMaps was of type HashTable but the getter/setter operated on a string datatype as input and this resulted in InvalidCastException.
System.InvalidCastException: Unable to cast object of type 'System.Collections.Hashtable' to type 'System.String'
at System.Windows.Markup.XmlAttributeProperties.GetXmlNamespaceMaps(DependencyObject dependencyObject)
at WpfApp.MainWindow in MainWindow.xaml.cs
Version
.NET 9 Preview 3
Previous behavior
Previously the GetXmlNamespaceMaps API was implemented as -
public static string GetXmlNamespaceMaps(DependencyObject dependencyObject)
{
if (dependencyObject == null)
{
throw new ArgumentNullException( "dependencyObject" );
}
return (string)dependencyObject.GetValue(XmlNamespaceMapsProperty);
}
Here the value returned by dependencyObject.GetValue(XmlNamespaceMapsProperty) is of Hashtable type and we were trying to type cast it to string which resulted in InvalidCastException.
And SetXmlNamespaceMaps API was implemented as following, here we were taking 2 parameters, one of type DependencyObject and another one was of string type.
public static void SetXmlNamespaceMaps(DependencyObject dependencyObject, string value)
{
if (dependencyObject == null)
{
throw new ArgumentNullException( "dependencyObject" );
}
dependencyObject.SetValue(XmlNamespaceMapsProperty, value);
}
As a result of the changes we made in GetXmlNamespaceMaps API, so here in SetXmlNamespaceMaps API we changed one of its parameter from string to Hashtable.
New behavior
The backing property of GetXmlNamespaceMaps has been changed from string to Hashtable.
public static Hashtable GetXmlNamespaceMaps(DependencyObject dependencyObject)
{
if (dependencyObject == null)
{
throw new ArgumentNullException( "dependencyObject" );
}
return (Hashtable)dependencyObject.GetValue(XmlNamespaceMapsProperty);
}
Type of breaking change
- Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
- Behavioral change: Existing binaries may behave differently at run time.
Reason for change
We aimed to address this issue specifically for .NET 9 Preview 3 and beyond, given that the proposed changes are straightforward, carry minimal risk, and ensure the continued functionality of the APIs.
Issue has been reported here.
Recommended action
Pass Hashtable instead of a string to SetXmlNamespaceMaps API.
Feature area
Windows Presentation Foundation (WPF)
Affected APIs
- XmlAttributeProperties.GetXmlNamespaceMaps
- XmlAttributeProperties.SetXmlNamespaceMaps