BLToolkit. Mapping ObjectToObject, without copy creation.

Problem in a trace: it is necessary to display one object on another (already created), types identical. For reference types to copy links. Found in BLToolkit class BLToolkit. Mapping. Map, it it does, but creates each time new object:




DestObject = BLToolkit.Mapping.Map.ObjectToObject<Template> (SourceObject); 

Hello, NNetman, you wrote:

NN> the Problem in a trace: it is necessary to display one object on another (already created), types identical. For reference types to copy links.


Then it is possible to receive simply all fields of object (Type:: GetFields (BindingFlags. Instance|BindingFlags. NonPublic)) to walk on them and to appropriate field values of one object to fields of another:




using System; 
using System. Reflection; 

class Program 
{ 
  static void Main (string [] args) { 
    Action source = () => Console. WriteLine ("Aaa"); 
    Action target = () => Console. WriteLine ("Bbb"); 
    target ();//"Bbb" 
    Copy (source, target); 
    target ();//"Aaa" 
  }

  static void Copy (object source, object target) { 
    if (source == null) { 
      throw new ArgumentNullException ("source"); 
    } else if (target == null) { 
      throw new ArgumentNullException ("target"); 
    } else if (source. GetType ()! = target. GetType ()) { 
      throw new ArgumentException ("source. GetType ()! = target. GetType ()", "target"); 
    }//if 

    var type = source. GetType (); 
    var fields = type. GetFields (BindingFlags. Instance | BindingFlags. NonPublic); 
    Array. ForEach (fields, field => field. SetValue (target, field. GetValue (source))); 
  }
}

Hello, NNetman, you wrote:

NN> the Problem in a trace: it is necessary to display one object on another (already created), types identical. For reference types to copy links. Found in BLToolkit class BLToolkit. Mapping. Map, it it does, but creates each time new object:
NN> DestObject = BLToolkit.Mapping.Map.ObjectToObject<Template> (SourceObject);



In the same place there is an overload which accepts destinationObject.

 Yes _FRED _, it is a variant, but reflection me does not arrange.

Hello, MozgC, you wrote:

MC> Hello, NNetman, you wrote:

NN>> the Problem in a trace: it is necessary to display one object on another (already created), types identical. For reference types to copy links. Found in BLToolkit class BLToolkit. Mapping. Map, it it does, but creates each time new object:
NN>> DestObject = BLToolkit.Mapping.Map.ObjectToObject<Template> (SourceObject);

MC> In the same place there is an overload which accepts destinationObject.



Yes, there there is an overload




public static T ObjectToObject <T> (object sourceObject, params object [] parameters); 



But here parameters, it is explicit not destinationObject that actually and was  , does not work.

Hello, NNetman, you wrote:

NN> Yes _FRED _, it is a variant, but reflection me does not arrange.


And by what image  at you  does?

Hello, _FRED _, you wrote:

_FR> Hello, NNetman, you wrote:

NN>> Yes _FRED _, it is a variant, but reflection me does not arrange.

_FR> And what image  at you  does?



It uses reflection once then on the fly collects the assembly and connects it.

Hello, NNetman, you wrote:

NN>>> Yes _FRED _, it is a variant, but reflection me does not arrange.
_FR>> And what image  at you  does?
NN> It uses reflection once then on the fly collects the assembly and connects it.


Aha, means all the same arranges you  
big_smile

The main thing in my answer was "to receive all fields of object, to walk on them and to appropriate field values of one object to fields of another". And  __ only on the first step to "receive" 
wink

Not on an example it was necessary to turn attention (the example only shows that algorithm basically the worker), and to think over implementation of algorithm approaching you - the data in a question contained insufficiently what to consider all that to you actually it is required.


It would be desirable faster: please, it is possible and instead of SetValue/GetValue to use more modern methods:



using System; 
using System. Linq; 
using System. Linq. Expressions; 
using System. Reflection; 

class Program 
{ 
  static void Main (string [] args) { 
    Action source = () => Console. WriteLine ("Aaa"); 
    Action target = () => Console. WriteLine ("Bbb"); 
    target ();//"Bbb" 
    Copy (source, target); 
    target ();//"Aaa" 
  }

  static void Copy <T> (T source, T target) { 
    if (source == null) { 
      throw new ArgumentNullException ("source"); 
    } else if (source. GetType ()! = typeof (T)) { 
      throw new ArgumentException ("source. GetType ()! = typeof (T)", "source"); 
    } else if (target == null) { 
      throw new ArgumentNullException ("target"); 
    } else if (source. GetType ()! = target. GetType ()) { 
      throw new ArgumentException ("source. GetType ()! = target. GetType ()", "target"); 
    }//if 

    var copy = CreateMapper <T> (); 
    copy (source, target); 
  }

  static Action <T, T> CreateMapper <T> () { 
    var source = Expression. Parameter (typeof (T)); 
    var target = Expression. Parameter (typeof (T)); 

    var type = typeof (T); 
    var assigns = 
      from field in type. GetFields (BindingFlags. Instance | BindingFlags. NonPublic) 
      let left = Expression. Field (target, field) 
      let right = Expression. Field (source, field) 
      select Expression. Assign (left, right); 

    var lambda = Expression. Lambda <Action <T, T>> (Expression. Block (assigns), source, target); 
    return lambda. Compile (); 
  }
}



It is necessary to finish only this example to digestible (well there Memoize to fasten on obtaining  to learn to cause  with dynamic type ... but it already behind frames of the given arguing).

Hello, NNetman, you wrote:

MC>> In the same place there is an overload which accepts destinationObject.
NN> Yes, there there is an overload


Track please for  in at writing of answers




NN> public static T ObjectToObject <T> (object sourceObject, params object [] parameters); 


NN> But here parameters, it is explicit not destinationObject that actually and was  , does not work.


There there is also such overload:



public static object ObjectToObject (object sourceObject 
[b], object destObject[/b], params object [] parameters) 

Here about " __ only on the first step" to receive "" is generally speaking not truly. Because, by call GetValue/SetValue each time is used meta data. And in the second example, yes... The lambda is compiled... But by its each call it will be caused type. GetFields (BindingFlags. Instance | BindingFlags. NonPublic) besides, each time - . About Assign I do not know as it works most likely on clever  the code , only in this case will be faster than in the first example.

Yes, really is such, found
smile  Thanks.

Hello, the Anonymous author, you wrote:

Here about " __ only on the first step" to receive "" is generally speaking not truly. Because, by call GetValue/SetValue each time is used meta data.


Under  it is understood GetFields. Concerning GetValue/SetValue try to think attentively all the same that I told in the message.

And in the second example, yes... The lambda is compiled... But by its each call it will be caused type. GetFields (BindingFlags. Instance | BindingFlags. NonPublic) besides, each time  .


By a lambda call "GetFields" each time will not be caused. If you consider differently, write the code which it shows 
smile 
And, generally, you up to the end to read my message worked?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值