2013-07-24 29 views
0

注入通用的IList我在命名空間Device.Control枚舉...從spring.net

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

而且我有一個類...

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

下面是我的配置XML爲spring.net ...

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

建設,並試圖注入MyStateManager對象後,spring.NET抱怨說,它不能設置個e對象的ManagingStates屬性。我得到這個錯誤...

錯誤創建名爲 'MyStateManager' 中所定義 '文件 [Spring.xml] 3' 線對象:錯誤設置屬性值: PropertyAccessExceptionsException(1個錯誤);嵌套0​​PropertyAccessExceptions是:[Spring.Core.TypeMismatchException: 無法類型的屬性值轉換[System.Collections.ArrayList] 所需類型 [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 +國家, SandboxConsole,版本= 1.0.0.0,文化=中立, 公鑰=空]]]財產 'ManagingStates' ......

我有點新Spring.NET,我想不通除了它不能將一個ArrayList注入到IList屬性中外,這裏列出的問題是什麼。 是否有可能在值爲枚舉類型的配置中創建列表?如果是這樣,怎麼樣?

回答

2

您必須指定element-type

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

其中命名空間是你的類和大會包含您的枚舉組件的命名空間。

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

ApplicationEnumerations +國家因爲您嘗試訪問一個內部類。

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

+0

工作正常!謝謝! – kevin628