[SPRNET-1470] Setting property of type IList<T> using <list/> without the @element-type specified fails. Created: 28/Sep/11  Updated: 15/Nov/11  Resolved: 15/Nov/11

Status: Resolved
Project: Spring.NET
Component/s: Spring-NET-CORE
Affects Version/s: 1.3.2
Fix Version/s: 2.0 M1

Type: Bug Priority: Minor
Reporter: Choy Rim Assignee: Steve Bohlen
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relate
is related to SPRNET-1471 Set IDictionary<K,V> property using n... Resolved

 Description   

If I try to set a property of type IList<T> using the <list> without the element-type attribute, the object factory fails with a TypeMismatchedException. That is, if my target class has a property like so

public IList<string> GenericStringIList

{ get; set; }

And I try to set it in my spring config with the following xml fragment

<property name="GenericStringIList">
<list>
<value>string1</value>
</list>
</property>

It fails with
[Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.ArrayList] to required type [System.Collections.Generic.IList`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] for property 'GenericStringList'., Inner Exception: Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.ArrayList] to required type [System.Collections.Generic.IList`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]] for property 'GenericStringList'.
at Spring.Core.TypeConversion.TypeConversionUtils.ConvertValueIfNecessary(Type requiredType, Object newValue, String propertyName) in C:\work\spring-net\src\Spring\Spring.Core\Core\TypeConversion\TypeConversionUtils.cs:line 215]

What is strange about this is that if the property is declared as a regular List<>, everything is fine.

public List<string> GenericStringIList { get; set; }

An even more minimal test like the following shows the problem more clearly

[Test]
public void ConvertArrayListToGenericIList() {
var xs = new ArrayList

{ "Mark Pollack" }

;
var ys = TypeConversionUtils.ConvertValueIfNecessary(typeof(IList<string>), xs, "ignored");
Assert.That(ys as IList<string>, Is.Not.Null);
var zs = (IList<string>)ys;
Assert.That(zs[0], Is.EqualTo("Mark Pollack"));
}

This also bombs for the same reason.



 Comments   
Comment by Choy Rim [ 28/Sep/11 ]

The reason it is a bug instead of an improvement is because it looks like it was supposed to support conversion from an ArrayList to IList<>. I mean it doesn't make sense to support automatic conversion to List<> and not IList<>.

Comment by Steve Bohlen [ 15/Nov/11 ]

Fixed in Pull 6 https://github.com/SpringSource/spring-net/pull/6

Generated at Tue Jul 23 09:32:18 UTC 2019 using JIRA 7.9.2#79002-sha1:3bb15b68ecd99a30eb364c4c1a393359bcad6278.