XML配置中的构造方法列表/数组类型参数

时间:2017-03-15 14:14:16

标签: autofac autofac-configuration

如何使用XML配置将数组/列表参数传递给类构造函数?

如果我想传递标量值,请使用以下配置:

public class ChartTabPageController {

    @FXML
    private LineChart<String, Double> lineChart;

    @FXML
    private CategoryAxis xAxis;

    private ObservableList<String> time = FXCollections.observableArrayList();

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {

        // Assign the month names as categories for the horizontal axis.
        xAxis.setCategories(time);

    }

    /**
     * Sets the persons to show the statistics for.
     * 
     * @param persons
     */
    public void setPriceData(List<PriceForward> prices) {
        int j = 0;

        for (int i = 0; i < 96; i++) {
            time.add(LocalTime.of(0, 0, 0).plusMinutes(15 * i).toString());
        }

        Double[] price = new Double[prices.size()];

        for (PriceForward p : prices) {
            price[j] = p.getPriceToday();
            j++;
        }

        XYChart.Series<String, Double> series = new XYChart.Series<>();

        // Create a XYChart.Data object for each month. Add it to the series.
        for (int i = 0; i < prices.size(); i++) {

            series.getData().add(new XYChart.Data<>(time.get(i), price[i]));

        }

        lineChart.getData().add(series);
    }
}

我的类的构造函数需要一个IEnumerable类型的参数。

祝你好运, arnam

2 个答案:

答案 0 :(得分:1)

简短版本:目前不支持。 Related issue here.

答案 1 :(得分:0)

我尝试过使用JSON配置,但参数未传递给构造函数。我的配置看起来像这样:

     "parameters":{
        "constructorParamName":["stringValue1", "stringValue2"]
     }

,构造函数是:

public MyClassConstructor(IList<string> constructorParamName = null) 

不幸的是“constructorParamName”值始终为null。

<强>更新

我发现问题是由IContainer.ResolveNamed方法引起的。此方法从配置文件传递属性,但不传递构造函数参数。