31 lines
884 B
C#
31 lines
884 B
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Sinvo.EplanHpD.Plugin.WPFUI.Utils
|
|||
|
{
|
|||
|
public class MapperUtil
|
|||
|
{
|
|||
|
public static T MapFor<T,S>(S sourceObj)
|
|||
|
{
|
|||
|
if (sourceObj == null)
|
|||
|
return default(T);
|
|||
|
var destObj = Activator.CreateInstance<T>();
|
|||
|
var sourceType = sourceObj.GetType();
|
|||
|
var destType = typeof(T);
|
|||
|
foreach (var prop in destType.GetProperties())
|
|||
|
{
|
|||
|
var sourceProp = sourceType.GetProperty(prop.Name);
|
|||
|
if (sourceProp != null && prop.CanWrite)
|
|||
|
{
|
|||
|
var value = sourceProp.GetValue(sourceObj);
|
|||
|
prop.SetValue(destObj, value);
|
|||
|
}
|
|||
|
}
|
|||
|
return destObj;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|