Last active
August 29, 2015 14:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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