Kmills94
1/2/2018 - 7:50 PM

Object Mapper


internal TOut ObjectMapper<TIn, TOut>(TIn inputObj)
{

    dynamic outputObj = Activator.CreateInstance(typeof(TOut));

    foreach (var inputObjProp in inputObj.GetType().GetProperties())

    {

        var retValProperty = outputObj.GetType().GetProperty(inputObjProp.Name);

        if (retValProperty != null && retValProperty.PropertyType == inputObjProp.PropertyType)

        {

            var inputPropertyValue = inputObjProp.GetValue(inputObj, null);

            retValProperty.SetValue(outputObj, inputPropertyValue, null);

        }

    }

    return outputObj;

}