I try to initialize typed IDictionary object property with xml Spring.net declaration, but exception occurs.
Here is class definition:
public class MyObjectClass
{
public IDictionary<string,string> Params { get; set; }
}
Corresponding fragment of spring xml configuration:
<object id="MyObject" type="MyObjectClass, MyAssembly">
<property name="Params">
<dictionary>
<entry key="Error" value="1"/>
<entry key="Warning" value="2"/>
<entry key="Information" value="4"/>
</dictionary>
</property>
</object>
Unfortunately it doesn’t work. Exception is thrown during spring initialization:
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.Specialized.HybridDictionary] to required type [System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] for property 'Params'., Inner Exception: Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.Specialized.HybridDictionary] to required type [System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] for property 'Params'.
in Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Core\TypeConversion\TypeConversionUtils.cs:175]
Source=Spring.Core
ExceptionCount=1
StackTrace:
in Spring.Objects.ObjectWrapper.SetPropertyValues(IPropertyValues propertyValues, Boolean ignoreUnknown) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\ObjectWrapper.cs:377
in Spring.Objects.ObjectWrapper.SetPropertyValues(IPropertyValues pvs) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\ObjectWrapper.cs:305
in Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ApplyPropertyValues(String name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Objects\Factory\Support\AbstractAutowireCapableObjectFactory.cs:384
I’m focused on passing list of key+value pairs to object with usage of xml style configuration.
You have to specify the key- and value type for the dictionary:
Without these type definitions, spring tries to instantiate a non-generic dictionary, which results in the
TypeMismatchExceptionfrom your error message: