using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Reflection; namespace CommonConsole { public static class ReflectionExtensions { private static readonly ConcurrentDictionary, Action>> metadata = new ConcurrentDictionary, Action>>(); public static void CopyTo(this T source, T destination) where T : class { var type = typeof(T); var properties = metadata.GetOrAdd(type, AddTypeProperties); foreach (var kvp in properties) { var getter = kvp.Key; var setter = kvp.Value; var value = getter(source, null); setter(destination, value, null); } } private static IDictionary, Action> AddTypeProperties(Type type) { var properties = new Dictionary, Action>(); foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (property.CanWrite) { properties[property.GetValue] = property.SetValue; } } return properties; } } }