简体   繁体   中英

Injecting a generic IList from spring.net

I have an enumeration in the namespace Device.Control ...

public enum State {
    IN_PROGRESS, SUSPENDED, HALTED, FINISHED
}

And I have a class...

public class CustomStateManager {
    public IList<State> ManagingStates { get; set; } 
}

And here's my config XML for spring.net...

<object id="MyStateManager" type="CustomStateManager">
    <property name="ManagingStates">
        <list>
            <value>SUSPENDED</value>
            <value>HALTED</value>
        </list>
    </property>
</object>

After building and trying to inject the MyStateManager object, Spring.NET complains that it cannot set the ManagingStates property of the object. I get this error...

Error creating object with name 'MyStateManager' defined in 'file [Spring.xml] line 3' : Error setting property values: PropertyAccessExceptionsException (1 errors); nested PropertyAccessExceptions are: [Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.ArrayList] to required type [System.Collections.Generic.IList 1[[SandboxConsole.Device.Control.ApplicationEnumerations+State, SandboxConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]] for property 'ManagingStates'., Inner Exception: Spring.Core.TypeMismatchException: Cannot convert property value of type [System.Collections.ArrayList] to required type [System.Collections.Generic.IList 1[[SandboxConsole.Device.Control.ApplicationEnumerations+State, SandboxConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]] for property 'ManagingStates'...

I am a little new to Spring.NET and I cannot figure out what the problem here is, other than it cannot inject an ArrayList into the IList property. Is it possible to create lists in the configuration where values are enumerated types? If so, how?

You have to specify the element-type :

<list element-type="Namespace.State, Assembly">
    <value>SUSPENDED</value>
    <value>HALTED</value>
</list>

where Namespace is the namespace of your class and Assembly the assembly that contains your enum.

<list element-type="SandboxConsole.Device.Control.ApplicationEnumerations+State, SandboxConsole">
    <value>SUSPENDED</value>
    <value>HALTED</value>
</list>

ApplicationEnumerations+State because you try to access an inner class.

http://www.springframework.net/doc-latest/reference/html/objects.html#objects-generic-collections-values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM