You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用DryIoc的Resolve方法传递字符串参数实例化ViewModel?

解决Prism DryIoc解析时传递字符串参数的问题

首先,你需要明确Resolve方法接收的是值元组数组,而非匿名类型或普通数组。正确的调用语法需要直接传递符合(Type Type, object Instance)格式的值元组。

正确调用示例

方式1:直接通过params传递值元组

var viewModel = containerProvider.Resolve<MainWindowViewModel>(
    (typeof(string), "你的字符串值")
);

方式2:显式传入值元组数组(多参数场景更清晰)

var parameters = new (Type Type, object Instance)[]
{
    (typeof(string), "你的字符串值")
};
var viewModel = containerProvider.Resolve<MainWindowViewModel>(parameters);

无效尝试的原因

  • 匿名类型new {Type=typeof(String), Instance="abc"}不符合方法要求的(Type, object)值元组类型,DryIoc无法识别并匹配构造函数参数。
  • 非值元组类型的普通数组,无法被方法的params参数正确接收。

多手动参数扩展

如果构造函数有多个需手动传递的参数,直接追加对应值元组即可:

var viewModel = containerProvider.Resolve<MainWindowViewModel>(
    (typeof(string), "第一个字符串"),
    (typeof(int), 123)
);

内容的提问来源于stack exchange,提问作者Martin Robins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.05 13:12:02