当前位置: 首页 > 编程语言 > C#

MVVMSimpleIoc,接口实现需要构造参数时如何使用接口共享

时间:2023-04-10 19:15:32 C#

MVVMSimpleIoc,接口实现需要构造参数时如何使用接口但是实现需要在其构造函数中有一个参数:publicclassMyServiceImplementation:IMyService{publicMyServiceImplementation(stringconstructorString){...}}我预计注册接口的实现会起作用,但SimpleIoc在尝试解析接口时不考虑提示。SimpleIoc.Default.Register(()=>{returnnewMyServiceImplementation("HelloWorld");});SimpleIoc.Default.Register();有没有办法用SimpleIoc做到这一点,或者我应该考虑使用更完整的Ioc?编辑:这确实有效,但我仍然想知道为什么上面的表格不起作用。SimpleIoc.Default.Register(()=>{returnnewMyServiceImplementation("HelloWorld");});您的第一种方法不起作用的原因是SimpleIOC不使用自身来构造对象。鉴于您的声明SimpleIoc.Default.Register(()=>{returnnewMyServiceImplementation("HelloWorld");});SimpleIoc.Default.Register();对SimpleIoc.Default.GetInstance()的调用将执行工厂方法,而对SimpleIoc.Default.GetInstance()的调用则不会。一种可能的链接调用方式是指定IMyService和MyServiceImplementation两种类型的工厂方法,即以上是C#学习教程:MVVMSimpleIoc,在接口实现需要的时候如何使用接口共享所有内容构造参数,如果你有使用,需要了解更多C#学习教程,希望大家多多关注—SimpleIoc.Default.Register(()=>{returnnewMyServiceImplementation("HelloWorld");});SimpleIoc.Default.Register(()=>{returnSimpleIoc.Default.GetInstance();});本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: