UnityContainer.BuildUp() - 仅当属性为空时,我可以让它将新实例注入到属性中吗?
我正在反序列化这样一个类
class AClass{
[Dependency]
AnotherClass Property{ get; set; }
}
,当我使用 BuildUp() 对象时,我希望 Unity 仅在属性为 null 时创建 AnotherClass 的新实例,否则仅对其执行 BuildUp。 有没有一种简单的方法可以实现这一目标?
编辑:我正在用 wpf 做 mvvm 。这些类是视图模型,我将它们序列化,因为我想在运行之间保留一些属性,并且它们还有一些我想要统一注入的依赖项。因此,在反序列化之后,嵌套模型已经存在并设置了属性,因此我不希望 Unity 用新实例覆盖它,但我仍然希望它在其上调用 InjectionMethods 并在程序第一次运行时正常解析它,嵌套模型为空。
I'm deserializing a class like this one
class AClass{
[Dependency]
AnotherClass Property{ get; set; }
}
When I then BuildUp() the object I'd like Unity to create a new instance of AnotherClass only if the property is null and otherwise just perform a BuildUp on it.
Is there a simple way to achieve this?
Edit: I'm doing mvvm with wpf. The classes are view-models, I serialize them as there are some properties I want to preserve between runs and they also have some dependencies I want unity to inject. So after deserialization the nested model is already there with properties set so I don't want unity to override it with a new instance but I still want it to call InjectionMethods on it and also to resolve it normally the first time the program is run and the nested models are null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终写了一个统一扩展。
我找不到太多关于如何执行此操作的文档,所以我不太确定结果。
它似乎有效,但我有这种感觉我可能写了一些可怕的东西..
无论如何,这是代码,欢迎改进:
I ended up writing a unity extension.
I could not find much documentation on how to do this so I'm not really sure about the result.
It seems to work but I have this feeling I might have written something horrible..
Anyway here's the code, improvements are welcome:
您可以编写自己的
BuildUp
方法,如下所示:用法:
我没有测试甚至没有编译此代码,但它应该可以解决问题。请注意,由于它每次都会迭代所有注册并使用反射注入所有属性,因此它可能不是最快的事情;-)
You can write your own
BuildUp
method, like this:Usage:
I didn't test or even compile this code, but it should pretty much do the trick. Note that since it iterates all
Registrations
every time and injects all properties using reflection, it might not be the fasted thing ;-)有简单的方法吗?不,
您需要编辑生成的 il,检测已经存在的对象...这是可以完成的,但绝不简单。
Is there a simple way? No.
You'd need to edit the generated il, detect already existing objects... It could be done but it's in no way simple.