internal static T Wrap <T>(this T instance, ProxyGenerator generator, Frozen frozen) where T : class { var interceptor = new FreezableInterceptor <T>(generator, instance, frozen); var freezable = generator.CreateClassProxy(typeof(T), new[] { typeof(IFreezable <T>), typeof(Freezer.IFreezableWrapper) }, interceptor); interceptor.SetWrappedInstance(freezable); return((T)freezable); }
public FreezableInterceptor(ProxyGenerator generator, T instance, Frozen frozen) { _generator = generator; _instance = instance; _frozen = frozen; var cloneMethod = _instance.GetType().GetMethod("Clone", BindingFlags.Public | BindingFlags.Instance); if (cloneMethod != null && cloneMethod.ReturnType == _instance.GetType() && !cloneMethod.IsGenericMethod && cloneMethod.GetParameters().Length == 0) { _cloneMethod = cloneMethod; } }
private object Clone(Frozen frozen) { T clone; if (_cloneMethod != null) { clone = (T)_cloneMethod.Invoke(_instance, new object[0]); } else { //clone = Deserializer.Deserialize<T>(Serializer.Serialize(_instance)); clone = Incubator.Clone(_instance, new[] { "IsFrozen" }); } return(clone.Wrap(_generator, frozen)); }
public ProxiedIEnumerable(IEnumerable <T> source, ProxyGenerator generator, Frozen frozen) { if (typeof(T).IsProxiableType()) { var list = new List <T>(); _values = list; foreach (var value in source) { list.Add((T)value.Wrap(generator, typeof(T), frozen)); } } else { _values = source; } }
public ProxiedIList(string propertyName, Type parentType, IList <T> source, ProxyGenerator generator, Frozen frozen) { _propertyName = propertyName; _parentType = parentType; _source = source; _generator = generator; _frozen = frozen; // TODO: assumes that no if (!typeof(T).IsProxiableType()) { return; } _usesProxies = true; _proxies = new List <T>(); foreach (var value in source) { _proxies.Add(Wrap(value)); } }
// TODO: should cache the constructors of these types internal static object Wrap(this object instance, ProxyGenerator generator, Type instanceType, Frozen frozen) { var interceptorType = typeof(FreezableInterceptor <>).MakeGenericType(instanceType); var interceptorCtor = interceptorType.GetConstructor(new[] { typeof(ProxyGenerator), instanceType, typeof(Frozen) }); var interceptor = (IInterceptor)interceptorCtor.Invoke(new[] { generator, instance, frozen }); var freezableType = typeof(IFreezable <>).MakeGenericType(instanceType); var freezable = generator.CreateClassProxy(instanceType, new[] { freezableType, typeof(Freezer.IFreezableWrapper) }, interceptor); ((IFreezableInterceptor)interceptor).SetWrappedInstance(freezable); return(freezable); }