Beispiel #1
0
 internal static object Create(Type workerType, ITransporter transporter, long serviceId, bool disposeWrapped = false)
 {
     CheckType(workerType);
     return(proxyGenerator.CreateInterfaceProxyWithoutTarget(
                workerType,
                new RemoteWorker(transporter, serviceId, disposeWrapped)));
 }
Beispiel #2
0
        /// <summary>
        /// Gets a stub instance of the specified type.
        /// </summary>
        /// <returns>A stub object.</returns>
        /// <typeparam name="T">The type of the stub to create.</typeparam>
        public T GetStub <T>() where T : class
        {
            InterfaceDetector.AssertIsInterface <T>();
            var interceptor = interceptorFactory();

            return(proxyGenerator.CreateInterfaceProxyWithoutTarget <T>(interceptor));
        }
        public TProxy Create <TProxy>()
            where TProxy : class
        {
            var dynamicProxyValueInterceptor = new DynamicProxyPropertyValueInterceptor(_proxyPropertyValueResolver);
            var dynamicProxy = !typeof(TProxy).IsInterface
                ? _proxyGenerator.CreateClassProxy <TProxy>(dynamicProxyValueInterceptor)
                : _proxyGenerator.CreateInterfaceProxyWithoutTarget <TProxy>(dynamicProxyValueInterceptor);

            return(dynamicProxy);
        }
Beispiel #4
0
        public object Wrap(Type interfaceType, object implicitImplementation)
        {
            if (implicitImplementation == null)
            {
                throw new ArgumentNullException(nameof(implicitImplementation));
            }

            var dictionary = _methodInfosCache[implicitImplementation.GetType()];
            var wrapper    = new ImplicitInterceptor(
                implicitImplementation, dictionary, this);

            return(_generator.CreateInterfaceProxyWithoutTarget(interfaceType, wrapper));
        }
Beispiel #5
0
        public T CreateServiceProxy <T>(RpcHandler handler, RpcMethodFixture fixture, IServiceLoadBalancingStrategy strategy)
        {
            var proxy = _proxyGenerator.CreateInterfaceProxyWithoutTarget(
                typeof(T),
                new ProxyGenerationOptions(),
                new IInterceptor[]
            {
                new ServiceProxyInterceptor(
                    typeof(T),
                    _serviceResolver,
                    handler,
                    fixture,
                    strategy)
            });

            return((T)proxy);
        }
Beispiel #6
0
 public ChartProxy(IProxyGenerator generator, IInterceptor interceptor)
 {
     this.generator   = generator;
     this.interceptor = interceptor;
     Chart            = generator.CreateInterfaceProxyWithoutTarget <TChart>(interceptor);
 }
Beispiel #7
0
        private object?ProxyValue(Type returnType, object value, PropertyInfo?property)
        {
            try
            {
                Current = this;
                // Interface
                if (returnType.IsInterface && (returnType.GetInterface("IEnumerable") != null || returnType.GetInterface("IList") != null))
                {
                    dynamic result = typeof(List <>).MakeGenericType(returnType.GenericTypeArguments)
                                     .GetConstructor(Array.Empty <Type>())?.Invoke(Array.Empty <object?>()) ??
                                     throw new ApplicationException();
                    if (value is not IList <object> list)
                    {
                        if (property != null && property.GetCustomAttribute <AsSingularAttribute>() != null)
                        {
                            result.Add(ProxyValue(returnType.GenericTypeArguments[0], value, property));
                        }
                        else
                        {
                            throw new ApplicationException("Array expected!");
                        }
                    }
                    else
                    {
                        foreach (var item in list)
                        {
                            var val = ProxyValue(returnType.GenericTypeArguments[0], item);
                            result.Add((dynamic?)val);
                        }
                    }

                    return(result);
                }

                if (value is string s)
                {
                    var repositories = returnType.GetCustomAttributes <RepositoryAttribute>().ToArray();
                    if (repositories != null && repositories.Any())
                    {
                        foreach (var repository in repositories)
                        {
                            var obj = TryGetFromRepository(repository.RepositoryPath, s);
                            if (obj != null)
                            {
                                return(obj);
                            }
                        }
                        throw new ApplicationException($"Object with key '{s}' not found in repository!");
                    }
                }


                var builderAttr = returnType.GetCustomAttribute <BuilderAttribute>(false);
                if (builderAttr != null)
                {
                    var builder = builderAttr.BuilderType == null?returnType.GetMethod("Build") : builderAttr.BuilderType.GetMethod("Build");

                    if (builder != null)
                    {
                        return(builder.Invoke(null, new[] { value }) ?? throw new NullReferenceException("Build() should never return null values!"));
                    }
                    else
                    {
                        throw new ApplicationException("Builder's Build() method not found!");
                    }
                }

                if (returnType.IsInterface && !(value is string))
                {
                    return(_generator.CreateInterfaceProxyWithoutTarget(returnType,
                                                                        new Interceptor(this, value)));
                }

                if (returnType.IsAbstract)
                {
                    return(_generator.CreateClassProxy(returnType, new Interceptor(this, value)));
                }

                var ctor = returnType.GetConstructor(new[] { typeof(DynamicMetaObject) });
                if (ctor != null)
                {
                    return(ctor.Invoke(new[] { value }));
                }

                // presumably some system type
                var underlyingType = Nullable.GetUnderlyingType(returnType);
                if (underlyingType != null)
                {
                    if (value == null)
                    {
                        return(null);
                    }
                    else
                    {
                        returnType = underlyingType;
                    }
                }
                if (returnType.GetInterface("IConvertible") != null && value is IConvertible convertible)
                {
                    return(convertible.ToType(returnType, CultureInfo.InvariantCulture));
                }
                if (value is ExpandoObject)
                {
                    throw new ApplicationException("Can't proxy value!");
                }
                if (!returnType.IsInstanceOfType(value))
                {
                    throw new ApplicationException("Invalid value type!");
                }
                return(value);
            }
 public T Create() => _proxyGenerator.CreateInterfaceProxyWithoutTarget <T>(new ContractInterceptor <T>(_methodDispatcher));
Beispiel #9
0
 /// <summary>Create proxy object.</summary>
 /// <param name="factory">Object factory.</param>
 /// <typeparam name="T">Proxied type.</typeparam>
 /// <returns>Proxy.</returns>
 public static T Create <T>(Func <Task <T> > factory) where T : class =>
 ProxyGenerator.CreateInterfaceProxyWithoutTarget <T>(
     new DeferredConstructorInterceptor <T>(factory));
Beispiel #10
0
        public static object Build(object source, Type targetType, GooseOptions options)
        {
            var interceptor = new GooseInterceptor(source, targetType, options);

            return(ProxyGenerator.CreateInterfaceProxyWithoutTarget(targetType, new[] { typeof(IGooseTyped) }, interceptor));
        }