ValueInjecter: how to ignore some properties while performing .InjectFrom<UnflatLoopValueInjection>(data)?

廉价感情. 提交于 2019-12-23 15:42:21

问题


I'm using ValueInjecter instead of AutoMapper. I'm trying to perform unflattening operation using

.InjectFrom<UnflatLoopValueInjection>(model)

It works, but I would also like to specity some properties to ignore during the unflattening operation, for example writing something like:

.InjectFrom<UnflatLoopValueInjection>(new IgnoreProperties("Prop1", "Prop2"), model)

or

.InjectFrom<UnflatLoopValueInjection>(model).IgnoreProperties("Prop1", "Prop2")

Any ideas?


回答1:


With the latest version of Omu.ValueInjecter (v3.1.1 as of this writing), this feature is built-in:

instanceA.InjectFrom(new LoopInjection(new[] { "Prop1", "Prop2" }), instanceB);

Prop1 and Prop2 will be ignored.

This feature may have existed in previous versions, but isn't in v2.3 for example.




回答2:


you can grab the source code for the UnflatLoopValueInjection and create your own injection which has this feature and whatever else you need.

here's the code for it: http://valueinjecter.codeplex.com/SourceControl/latest#ValueInjecter/UnflatLoopValueInjection.cs

you could add a Property Ignored properties or put it in the constructor, and where you have the line

 var prop = sourceProp;

 //add
 if(ignoredProps.Contains(prop) continue;

this should give you a quick idea of what you can do



来源:https://stackoverflow.com/questions/20748971/valueinjecter-how-to-ignore-some-properties-while-performing-injectfromunflat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!