static object CreateComputeMethodIndexProxy(Type subjectType) { var pcd = new ProxyClassDescriptor(new ComputeMethodIndexMixin(subjectType)); var type = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); return(Activator.CreateInstance(type)); }
public object MakeDuckProxyFor(Type subjectType, object realSubject, ConcurrentDictionary <Type, Func <object, object> > realSubjectCtorCache) { if (realSubject == null) { return(null); } var realSubjectType = realSubject.GetType(); Func <object, object> ctor; if (!realSubjectCtorCache.TryGetValue(realSubjectType, out ctor)) { if (subjectType.IsAssignableFrom(realSubjectType)) { realSubjectCtorCache.GetOrAdd(realSubjectType, ReturnSelf); return(realSubject); } var pcd = new ProxyClassDescriptor( new StaticFactoryMixin(), new MethodExistsProxyMetaMixin(), new RealSubjectMixin(realSubjectType, new DuckProxySubject(subjectType))); Type proxyType = _proxyModule.GetTypeFromProxyClassDescriptor(pcd); ctor = realSubjectCtorCache.GetOrAdd(realSubjectType, proxyType != null ? GetCtorForType(proxyType) : ReturnNull); } return(ctor(realSubject)); }
public ISubjectMethodExists <T> MakeSubjectMethodExistsForDuckProxy <T>(Type realSubjectType) where T : class { var pcd = new ProxyClassDescriptor(new EmptyMixin( new SubjectMethodExistsForDuckProxySubject(typeof(T), realSubjectType))); var proxyType = _proxyModule.GetTypeFromProxyClassDescriptor(pcd); return((ISubjectMethodExists <T>)Activator.CreateInstance(proxyType)); }
public static T WrapDictionary <T>(this ProxyModule proxyModule, IDictionary <string, string> source) { var pcd = new ProxyClassDescriptor( new DynamicPropertySourceMixin(new DynamicPropertySourceSubject(typeof(T)))); Type proxyType = proxyModule.GetTypeFromProxyClassDescriptor(pcd); return((T)Activator.CreateInstance(proxyType, new object[] { new StringDictionaryDynamicPropertySource(source) })); }
public static ProxyClassDescriptor CreateSafeNullProxyDescriptorFor(Type type) { var pcd = new ProxyClassDescriptor( new StaticInstanceMixin(), new EmptyMixin(new SafeNullProxySubject(type), new SafeProxyMetaSubject()), new MethodExistsProxyMetaMixin()); return(pcd); }
public override void Initialize(ProxyClassDescriptor pcd) { var interfaces = new HashSet <Type>(pcd.Mixins.SelectMany(m => m.Subjects).Select(s => s.Type)); _addedSubjects = (from type in _realSubjectType.GetInterfaces() where !interfaces.Contains(type) select new DirectProxySubject(type)).ToList(); base.Initialize(pcd); }
public void CanCreateCoder() { var subject = new DuckProxySubject(typeof(IConvertible)); var mixin = new RealSubjectMixin(typeof(object), subject); var pcd = new ProxyClassDescriptor(mixin); var mixinCoder = mixin.CreateCoder(); Assert.That(subject.CreateCoder(mixinCoder, new NullProxyCodeBuilder()), Is.Not.Null); }
public void CanCreateCoder() { var subject = new MethodExistsProxyMetaSubject(); var mixin = new EmptyMixin(subject); var pcd = new ProxyClassDescriptor(mixin); var mixinCoder = mixin.CreateCoder(); Assert.That(subject.CreateCoder(mixinCoder, new NullProxyCodeBuilder()), Is.Not.Null); }
public void CanCreateCoder() { var subject = new DynamicPropertySourceSubject(typeof(IConvertible)); var mixin = new DynamicPropertySourceMixin(subject); var pcd = new ProxyClassDescriptor(mixin); var mixinCoder = mixin.CreateCoder(); Assert.That(subject.CreateCoder(mixinCoder, new NullProxyCodeBuilder()), Is.Not.Null); }
public void MultipleTypesArePartOfSameModule() { var typeA = CreateSampleProxyType(); var pcd = new ProxyClassDescriptor( new RealSubjectMixin(typeof(object))); var typeB = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); Assert.That(typeB.Module(), Is.SameAs(typeA.Module())); }
public static T MakePropertyStoreFor <T>(this ProxyModule proxyModule) { var pcd = new ProxyClassDescriptor( new EmptyMixin(new PropertyStoreSubject(typeof(T))), new MethodExistsProxyMetaMixin()); Type proxyType = proxyModule.GetTypeFromProxyClassDescriptor(pcd); return((T)Activator.CreateInstance(proxyType, new object[] {})); }
public void FactoryForDefaultCtor() { var pcd = new ProxyClassDescriptor( new StaticFactoryMixin(typeof(Sample).GetConstructor(Type.EmptyTypes))); var proxyType = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); var ctor = StaticFactoryMixin.GetCtor<Func<object>>(proxyType); var result = ctor(); Assert.That(result, Is.Not.Null); Assert.That(result, Is.TypeOf<Sample>()); }
public void CanDirectProxyAGenericIndexProperty() { var pcd = new ProxyClassDescriptor(new RealSubjectMixin(typeof(SampleIndexProperty <int>))); var type = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); var test = (ISampleIndexProperty <int>)Activator.CreateInstance(type, new SampleIndexProperty <int>()); Assert.That(test, Is.Not.TypeOf <SampleIndexProperty <int> >()); test[1] = 42; Assert.That(test[1], Is.EqualTo(42)); }
public void FactoryForDefaultCtor() { var pcd = new ProxyClassDescriptor( new StaticFactoryMixin(typeof(Sample).GetConstructor(Type.EmptyTypes))); var proxyType = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); var ctor = StaticFactoryMixin.GetCtor <Func <object> >(proxyType); var result = ctor(); Assert.That(result, Is.Not.Null); Assert.That(result, Is.TypeOf <Sample>()); }
public void DefaultInstanceIsNotThreadUnique() { var pcd = new ProxyClassDescriptor(new StaticInstanceMixin()); var proxyType = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); object instance = StaticInstanceMixin.GetInstanceValueFor(proxyType); var task = Task.Factory.StartNew(() => StaticInstanceMixin.GetInstanceValueFor(proxyType)); task.Wait(); object instance2 = task.Result; Assert.That(instance, Is.TypeOf(proxyType)); Assert.That(instance, Is.SameAs(instance2)); }
internal static DuckValueBindingOption TryBind(Type fromType, Type toType) { if (!toType.IsInterface || !fromType.IsSealed) return null; var pcd = new ProxyClassDescriptor( new StaticFactoryMixin(), new MethodExistsProxyMetaMixin(), new RealSubjectMixin(fromType, new DuckProxySubject(toType))); return pcd.IsValid() ? new StaticDuckCastValueBinding(pcd,fromType) : null; }
public void CanDirectProxyUniquePrivateProperties() { var pcd = new ProxyClassDescriptor(new RealSubjectMixin(typeof(SampleUniquePrivateProperties))); var type = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); var test = (ISampleProperty1)Activator.CreateInstance(type, new SampleUniquePrivateProperties()); Assert.That(test, Is.Not.TypeOf <SampleUniquePrivateProperties>()); Assert.That(test.Answer, Is.EqualTo(41)); var test2 = (ISampleProperty2)test; Assert.That(test2.Answer, Is.EqualTo(43)); }
public void DefaultInstanceIsNotThreadUnique() { var pcd = new ProxyClassDescriptor(new StaticInstanceMixin()); var proxyType = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); object instance = StaticInstanceMixin.GetInstanceValueFor(proxyType); var task = Task.Factory.StartNew(() => StaticInstanceMixin.GetInstanceValueFor(proxyType), TaskCreationOptions.LongRunning); task.Wait(); object instance2 = task.Result; Assert.That(instance, Is.TypeOf(proxyType)); Assert.That(instance, Is.SameAs(instance2)); }
internal static DuckValueBindingOption TryBind(Type fromType, Type toType) { if (!toType.IsInterface() || !fromType.IsSealed()) { return(null); } var pcd = new ProxyClassDescriptor( new StaticFactoryMixin(), new MethodExistsProxyMetaMixin(), new RealSubjectMixin(fromType, new DuckProxySubject(toType))); return(pcd.IsValid() ? new StaticDuckCastValueBinding(pcd, fromType) : null); }
void EmitCtorForSafeDirectProxy(ILGenerator gen, Type returnType) { // The argument for the constructor is already on the stack (which is the return value // from the method call on the real subject). var pcd = new ProxyClassDescriptor( new RealSubjectMixin(returnType, new SafeDirectProxySubject(returnType), new SafeProxyMetaSubject())); IFooType proxyType = _proxyModule.GetTypeFromProxyClassDescriptor(pcd); var ctor = proxyType.GetConstructor(new[] { returnType }); // ReSharper disable once AssignNullToNotNullAttribute gen.Emit(OpCodes.Newobj, ctor); // Needed for PEVerify gen.Emit(OpCodes.Castclass, returnType); }
public object GetSafeProxyForType(Type type, object target) { Func <object, object> ctor; if (!_safeCtorCache.TryGetValue(type, out ctor)) { var pcd = new ProxyClassDescriptor( new StaticFactoryMixin(), new RealSubjectMixin(type, new SafeDirectProxySubject(type), new SafeProxyMetaSubject()), new MethodExistsProxyMetaMixin()); Type proxyType = _proxyModule.GetTypeFromProxyClassDescriptor(pcd); ctor = _safeCtorCache.GetOrAdd(type, StaticFactoryMixin.GetCtor <Func <object, object> >(proxyType)); } return(ctor(target)); }
public object MakeDuckProxyForInterfaces(object realSubject, params Type[] interfaces) { var pcd = new ProxyClassDescriptor( new StaticFactoryMixin(), new MethodExistsProxyMetaMixin(), new RealSubjectMixin(realSubject.GetType(), interfaces.Select(a => (ISubjectDescriptor) new DuckProxySubject(a)).ToArray())); Type proxyType = _proxyModule.GetTypeFromProxyClassDescriptor(pcd); if (proxyType == null) { return(null); } var ctor = _proxyCtorByType.GetOrAdd(proxyType, GetCtorForType); return(ctor(realSubject)); }
public IFooType GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd) { IFooTypeBuilder result; if (_typesInProgress.TryGetValue(pcd, out result)) { return(result); } if (!pcd.IsValid()) { return(null); } var pcc = new ProxyClassCoder(this, pcd); return(new FooTypeFromType(pcc.Generate(t => _typesInProgress.Add(pcd, t)))); }
public override void Initialize(ProxyClassDescriptor pcd) { var ctors = _interceptorType.GetConstructors(); if (ctors.Length > 1) { throw new InvalidOperationException("An interceptor type must have a single constructor."); } var arg = ctors.First().GetParameters().FirstOrDefault(); if (arg == null || !arg.ParameterType.IsAssignableFrom(pcd.BaseClassType)) { throw new InvalidOperationException( "An interceptor type constructor must begin with a type assignable from the proxy base class type."); } base.Initialize(pcd); }
public virtual void Initialize(ProxyClassDescriptor pcd) { foreach (var subject in _subjects) subject.Initialize(this); }
Type CreateProxyType(ProxyClassDescriptor pcd) { var fooType = new ProxyModuleCoderAccess(this).GetTypeFromProxyClassDescriptor(pcd); return fooType!=null ? fooType.AsType() : null; }
public IFooType GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd) { IFooTypeBuilder result; if (_typesInProgress.TryGetValue(pcd, out result)) return result; if (!pcd.IsValid()) return null; var pcc = new ProxyClassCoder(this, pcd); return new FooTypeFromType(pcc.Generate(t => _typesInProgress.Add(pcd, t))); }
public Type GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd) { return _proxyClassTypes.GetOrAdd(pcd, CreateProxyType); }
IFooType IProxyModuleCoderAccess.GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd) { return new FooTypeFromType(GetTypeFromProxyClassDescriptor(pcd)); }
Type CreateProxyType(ProxyClassDescriptor pcd) { var fooType = new ProxyModuleCoderAccess(this).GetTypeFromProxyClassDescriptor(pcd); return(fooType != null?fooType.AsType() : null); }
public void CanDirectProxyAnIndexProperty() { var pcd = new ProxyClassDescriptor(new RealSubjectMixin(typeof(SampleIndexProperty))); var type = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); var test = (ISampleIndexProperty)Activator.CreateInstance(type, new SampleIndexProperty()); Assert.That(test, Is.Not.TypeOf<SampleIndexProperty>()); test[1] = 42; Assert.That(test[1], Is.EqualTo(42)); }
public void CanDirectProxyUniquePrivateProperties() { var pcd = new ProxyClassDescriptor(new RealSubjectMixin(typeof(SampleUniquePrivateProperties))); var type = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); var test = (ISampleProperty1)Activator.CreateInstance(type, new SampleUniquePrivateProperties()); Assert.That(test, Is.Not.TypeOf<SampleUniquePrivateProperties>()); Assert.That(test.Answer, Is.EqualTo(41)); var test2 = (ISampleProperty2)test; Assert.That(test2.Answer, Is.EqualTo(43)); }
public void MultipleTypesArePartOfSameModule() { var typeA = CreateSampleProxyType(); var pcd = new ProxyClassDescriptor( new RealSubjectMixin(typeof(object))); var typeB = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); Assert.That(typeB.Module, Is.SameAs(typeA.Module)); }
public void CanReproduceClassConstraintCorrectly() { var pcd = new ProxyClassDescriptor(new RealSubjectMixin(typeof(Sample), new DirectProxySubject(typeof(ISample)))); ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); }
StaticDuckCastValueBinding(ProxyClassDescriptor pcd, Type fromType) { _pcd = pcd; _fromType = fromType; }
static Type CreateSampleProxyType() { var pcd = new ProxyClassDescriptor(new EmptyMixin()); return(ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd)); }
public Type GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd) { return(_proxyClassTypes.GetOrAdd(pcd, CreateProxyType)); }
void EmitCtorForSafeDirectProxy(ILGenerator gen, Type returnType) { // The argument for the constructor is already on the stack (which is the return value // from the method call on the real subject). var pcd = new ProxyClassDescriptor( new RealSubjectMixin(returnType, new SafeDirectProxySubject(returnType), new SafeProxyMetaSubject())); IFooType proxyType = _proxyModule.GetTypeFromProxyClassDescriptor(pcd); var ctor = proxyType.GetConstructor(new[] {returnType}); // ReSharper disable once AssignNullToNotNullAttribute gen.Emit(OpCodes.Newobj, ctor); // Needed for PEVerify gen.Emit(OpCodes.Castclass, returnType); }
static Type CreateSampleProxyType() { var pcd = new ProxyClassDescriptor(new EmptyMixin()); return ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd); }
public override void Initialize(ProxyClassDescriptor pcd) { var interfaces = new HashSet<Type>(pcd.Mixins.SelectMany(m => m.Subjects).Select(s => s.Type)); _addedSubjects = (from type in _realSubjectType.FindInterfaces((t, c) => true, null) where !interfaces.Contains(type) select new DirectProxySubject(type)).ToList(); base.Initialize(pcd); }
IFooType IProxyModuleCoderAccess.GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd) { return(new FooTypeFromType(GetTypeFromProxyClassDescriptor(pcd))); }