Recently encountered the assignment problem between two classes, the properties of two classes are almost the same, so write a method to get the property by reflection and then assign the value of the property of a class to assign the value of the other class.
The framework is. NET 4.5
public static D Mapper<d, s> (s)
{
D d = activator.createinstance<d> ();
Try
{
var stype = S.gettype ();
var dtype = typeof (D);
foreach (PropertyInfo SP in Stype.getproperties ())
{
foreach (PropertyInfo DP in Dtype.getproperties ()
) {
if (dp.name = = Sp.name)
{
Dp.setvalue (d, Sp.getvalue (s))
;
}}} catch (Exception ex)
{
} return
D;
}
The above is a small set up to introduce C # Two of the same attributes of the class assignment method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!