コード例 #1
0
        static object CreateComputeMethodIndexProxy(Type subjectType)
        {
            var pcd  = new ProxyClassDescriptor(new ComputeMethodIndexMixin(subjectType));
            var type = ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd);

            return(Activator.CreateInstance(type));
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        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) }));
        }
コード例 #5
0
ファイル: SafeFactory.cs プロジェクト: davidvmckay/ProxyFoo
        public static ProxyClassDescriptor CreateSafeNullProxyDescriptorFor(Type type)
        {
            var pcd = new ProxyClassDescriptor(
                new StaticInstanceMixin(),
                new EmptyMixin(new SafeNullProxySubject(type), new SafeProxyMetaSubject()),
                new MethodExistsProxyMetaMixin());

            return(pcd);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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()));
        }
コード例 #11
0
        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[] {}));
        }
コード例 #12
0
 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>());
 }
コード例 #13
0
        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));
        }
コード例 #14
0
        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>());
        }
コード例 #15
0
 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));
 }
コード例 #16
0
        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;
        }
コード例 #17
0
        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));
        }
コード例 #18
0
        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));
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
ファイル: SafeFactory.cs プロジェクト: davidvmckay/ProxyFoo
        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));
        }
コード例 #22
0
        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));
        }
コード例 #23
0
            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))));
            }
コード例 #24
0
        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);
        }
コード例 #25
0
ファイル: MixinBase.cs プロジェクト: davidvmckay/ProxyFoo
 public virtual void Initialize(ProxyClassDescriptor pcd)
 {
     foreach (var subject in _subjects)
         subject.Initialize(this);
 }
コード例 #26
0
ファイル: ProxyModule.cs プロジェクト: davidvmckay/ProxyFoo
 Type CreateProxyType(ProxyClassDescriptor pcd)
 {
     var fooType = new ProxyModuleCoderAccess(this).GetTypeFromProxyClassDescriptor(pcd);
     return fooType!=null ? fooType.AsType() : null;
 }
コード例 #27
0
ファイル: ProxyModule.cs プロジェクト: davidvmckay/ProxyFoo
            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)));
            }
コード例 #28
0
ファイル: ProxyModule.cs プロジェクト: davidvmckay/ProxyFoo
 public Type GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd)
 {
     return _proxyClassTypes.GetOrAdd(pcd, CreateProxyType);
 }
コード例 #29
0
ファイル: ProxyModule.cs プロジェクト: davidvmckay/ProxyFoo
 IFooType IProxyModuleCoderAccess.GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd)
 {
     return new FooTypeFromType(GetTypeFromProxyClassDescriptor(pcd));
 }
コード例 #30
0
        Type CreateProxyType(ProxyClassDescriptor pcd)
        {
            var fooType = new ProxyModuleCoderAccess(this).GetTypeFromProxyClassDescriptor(pcd);

            return(fooType != null?fooType.AsType() : null);
        }
コード例 #31
0
 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));
 }
コード例 #32
0
 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));
 }
コード例 #33
0
 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));
 }
コード例 #34
0
        public void CanReproduceClassConstraintCorrectly()
        {
            var pcd = new ProxyClassDescriptor(new RealSubjectMixin(typeof(Sample), new DirectProxySubject(typeof(ISample))));

            ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd);
        }
コード例 #35
0
 StaticDuckCastValueBinding(ProxyClassDescriptor pcd, Type fromType)
 {
     _pcd      = pcd;
     _fromType = fromType;
 }
コード例 #36
0
 StaticDuckCastValueBinding(ProxyClassDescriptor pcd, Type fromType)
 {
     _pcd = pcd;
     _fromType = fromType;
 }
コード例 #37
0
        static Type CreateSampleProxyType()
        {
            var pcd = new ProxyClassDescriptor(new EmptyMixin());

            return(ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd));
        }
コード例 #38
0
 public Type GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd)
 {
     return(_proxyClassTypes.GetOrAdd(pcd, CreateProxyType));
 }
コード例 #39
0
 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);
 }
コード例 #40
0
 static Type CreateSampleProxyType()
 {
     var pcd = new ProxyClassDescriptor(new EmptyMixin());
     return ProxyModule.Default.GetTypeFromProxyClassDescriptor(pcd);
 }
コード例 #41
0
 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);
 }
コード例 #42
0
 IFooType IProxyModuleCoderAccess.GetTypeFromProxyClassDescriptor(ProxyClassDescriptor pcd)
 {
     return(new FooTypeFromType(GetTypeFromProxyClassDescriptor(pcd)));
 }