Example #1
0
        object CreateNullProxyForType(Type type)
        {
            var  pcd       = CreateSafeNullProxyDescriptorFor(type);
            Type proxyType = _proxyModule.GetTypeFromProxyClassDescriptor(pcd);

            return(StaticInstanceMixin.GetInstanceValueFor(proxyType));
        }
Example #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));
        }
        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 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[] {}));
        }