c# - What is the best way to map two objects of the same type excluding some fields? -


I've already posted your question but I had no reason - let me think - this is quite normal I more concise I will try

I have two objects of the same type and I want to map some properties and want to exclude others. What I am trying to do, saves an object in a cache and then receives it, using only the attribute attributes (fields).

I have taken a look at Automapers , but I have not got anything suitable for me, so I have thought of implementing my system.
I've created a feature:

  [AttributeUsage (AttributeTargets.Property, AllowMultiple = false, Inherited = false)) public class FilterFieldAttribute: attribute {}   

and decorated to a class with those fields in which I would need to include:

  public class order Dekenmodel: Beswu model {... [FilterField] [DisplayName ( "Order Number:")] Received Public String Serial Number { Set; } [FilterField] [DisplayName ("From Date:")] Date Received From Public Date Time; Set; } [FilterField] [DisplayName ("To date:")] Public Datetime Tundet {get; Set; } [DisplayName ("Status:")] Get Public Uncertainty Status; Set; } ...   

Now, I have implemented a function that is responsible for mapping:

  Private T map & lt; T & gt; (T Source, T destination) where T: base visual model {if (source == blank) {return (source); FilterFiltAttribute filterAttribute; Forchha (Proptiinfoid KGetProperties (Propinfo type type (T))) {foreach (ProperInfo.GetCustomAttributes) in FilterFieldAttribute Atrr (Taipf (FilterFieldAttribute), false)) as {filterAttribute = attr FilterFieldAttribute; If (filterAttribute! = Null) {var value = propInfo.GetValue (source, empty); Propinfo.SetValue (destination, value, zero); }}} Return (Destination); }   

Now, when I need to get your view-MOIL cache and fill the marked properties with only attribute my code looks like this:

  viewmodel = map & lt; T & gt; (MyCache.Get (key) as T, ViewModel);   

I do not know that it is best to do this, but it seems that I have found the only way.
Any suggestions would be appreciated.

As the use of direct reflection (for example) would be sloooow ; The more complete answer is difficult, because it depends on whether or not you want a deep clone of upper clones or sub-objects. Either way, you should expect that some meta programming and caching - either use ILGenerator or expression.

However, for a lazy alternative, the serialization can be useful. Many serializers allow you to use special features to include / remove special items; By serializing in a memory-stream, rewinding ( .osition = 0 ) and deserializing you should get a deep copy of your chosen members.

Here is an example using the . Expression :

  Using the system; Using System.ComponentModel; Using System.Linq; Using System.Linq.Expressions; [AttributeUsage (AttributeTargets.Property, AllowMultiple = false, Traditionally derived = false)] Public class FilterFieldAttribute: Properties {public static t clone & lt; T & gt; (T-obj) where T: square, new () {return cache; T & gt; .clone (obj); } Private Static Cash & lt; T & gt; Where T: Class, New () {Funk for public static reading & lt; T, T & gt; Clone; Static cache () {var param = expression. Parameter (typef (t), "source"); Var member = for prop to typef (t). GateProperties () where feature ISDiffinid (Prop, Typef (Filter FileEditor)) Expression Choose bind (prop, expression. Property (ultimate, prop)); Var newObj = expression.memberinit (expression.New (typef (t)), member); Clone = Expression.Lambda & LT; & Lt; T, T & gt; & Gt; (NewObj, Ultimate) .Compile (); }}} Public Category OrdersViewModel {[FilterField] [DisplayName ("Order Number:")] Public string ORDERNUMBER {get; Set; } [FilterField] [DisplayName ("From Date:")] Date Received From Public Date Time; Set; } [FilterField] [DisplayName ("To date:")] Public Datetime Tundet {get; Set; } [DisplayName ("Status:")] Get Public Uncertainty Status; Set; } Static void main () {var foo = New OrdersViewModel {ORDERNUMBER = "abc", FromDate = DateTime.Now, todate = DateTime.Now, position = 1}; Var bar = FilterFieldAttribute.Clone (foo); }}    

Comments