簡體   English   中英

如何在XML配置中添加字符串數組組件?

[英]How to add string array component to XML configuration?

我在XML配置文件中有許多描述不同配置的組件。 每個配置都有一個字符串數組屬性IgnoredCountries 現在,我必須使所有國家/地區都忽略某些配置。 為了減小配置文件的大小(並使其更全面),我想創建單獨的組件allCountries ,其中將包含所有國家/地區代碼,並在需要時引用它。 問題是為字符串數組正確定義組件。

我正在使用溫莎城堡作為IoC容器。 我嘗試使用以下代碼聲明該組件,但未成功。

<component id="allCountries" service="System.String[], mscorlib">
  <parameters>
    <collection>
      <list>
        <item>AF</item>
        <item>AL</item>
        <item>AS</item>
        <!-- there's a lot of items -->
      </list>
    </collection>
  </parameters>
</component>

當嘗試像上面顯示的那樣聲明組件時,我收到一個異常,說組件allCountries is waiting for the following dependencies: Parameter '' which was not provided. Did you forget to set the dependency? is waiting for the following dependencies: Parameter '' which was not provided. Did you forget to set the dependency?

這不起作用,因為stringstring[]不是有效的組件類型。 組件應該是類,而不是基元。

理想情況下,您不會為此使用XML,但是如果確定在您的情況下有意義,則可以將其放在xml屬性中。 我不是100%肯定會奏效,但請嘗試一下。 請參閱此處的文檔中的示例。

一個更干凈的解決方案是將其包裝在一個類中,而不是依賴於string[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM