Mandar's blog about .Net & PHP Web Development

  • Archive
  • RSS

.NET Tip : How to copy all property values from One class to another?

Do you ever wanted to copy ALL Property Values from one .net Class to another (Whilst Both of them having properties with similar names)?

Heres Method for that :

public static void CopyPropertyValues(object source, object destination)
{
Type sourceType = source.GetType();
Type destinationType = destination.GetType();

foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
{
PropertyInfo destinationProperty =     destinationType.GetProperty(sourceProperty.Name);
if (destinationProperty != null)
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
}
}

  • 2 years ago
  • Permalink
  • Share
    Tweet
← Previous • Next →

Get 2GB Free Cloud storage

>> Download dropbox


About

I am programmer from London specializing in web based technologies, startups and web 2.0 related stuff.

My Startup

WealthBird

Available for Freelancing work

I am available for .NET contracting work in & around London. Drop me a mail : mandarp1979 at gmail.com

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr