Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static readonly Dictionary<Type, Func<object, object>> CloneFuncCache =
- new Dictionary<Type, Func<object, object>>();
- private object Clone()
- {
- var type = GetType();
- if (!CloneFuncCache.ContainsKey(type))
- {
- var newExpression = Expression.New(type);
- var sourceParam = Expression.Parameter(typeof(object));
- var srcObject = Expression.Convert(sourceParam, type);
- var propertyBindings = from propertyInfo in type.GetBindingProperties()
- let srcProperty = Expression.Property(srcObject, propertyInfo)
- select Expression.Bind(propertyInfo, srcProperty);
- var ex = Expression.Lambda<Func<object, object>>(
- Expression.MemberInit(newExpression, propertyBindings),
- new[] {sourceParam});
- CloneFuncCache.Add(type, ex.Compile());
- }
- return CloneFuncCache[type](this);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement