Skip to content

Instantly share code, notes, and snippets.

@jltrem
Last active August 29, 2015 14:04
Show Gist options
  • Save jltrem/9d4e0d60c990e9293c85 to your computer and use it in GitHub Desktop.
Save jltrem/9d4e0d60c990e9293c85 to your computer and use it in GitHub Desktop.
method to create an object and copy the properties from another object into it
// expected usage: var tableRec = Utils.CopyProperties<StatDbRecord>(fileRow, typeof(IStat));
static public class Utils
{
static public TDst CopyProperties<TDst>(object src, Type hasProps) where TDst : class, new()
{
var dst = new TDst();
foreach (var propInf in hasProps.GetProperties())
{
var val = propInf.GetValue(src, null);
propInf.SetValue(dst, val, null);
}
return dst;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment