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](http://www.progtown.com/img/smilies/big_smile.png)
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](http://www.progtown.com/img/smilies/wink.png)
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).