Example #1
0
        public object Create(IComponentContext ctx, object instance, params object[] constructorArguments)
        {
            var contracts = ctx.Component.Contracts.Where(i => !string.IsNullOrEmpty(i.FullName)).ToArray();

            if (ctx.GenericParameters != null)
            {
                for (int i = 0; i < contracts.Length; i++)
                {
                    if (contracts[i].IsOpenGenericType() && contracts[i].GetGenericArguments().Length == ctx.GenericParameters.Length)
                    {
                        contracts[i] = contracts[i].MakeGenericType(ctx.GenericParameters);
                    }
                }
            }

            var matchMethods = (ctx.Component.ExtendedProperties["methods"] as MethodInfo[]).Select(p => p.Name).ToArray();

            var baseInterfaces = contracts.Where(i => i.IsInterface).ToArray();

            var invocationHandler = new SimpleInvocationHandler(matchMethods);

            var impType = ctx.Component.Implementation;

            if (impType.IsAbstract || impType.IsInterface)
            {
                throw new ActivatorException(
                          string.Format(Mini_Resources.TypeAbstract,
                                        impType.FullName,
                                        ctx.Component.Contracts[0].FullName));
            }

            if (impType.IsOpenGenericType())
            {
                impType = impType.MakeCloseGenericType(ctx.GenericParameters);
            }


            var proxy = NLite.Reflection.Proxy.NewProxyInstance(impType, baseInterfaces,
                                                                invocationHandler, constructorArguments
                                                                );

            return(proxy);
        }
Example #2
0
        public object Create(IComponentContext ctx, object instance, params object[] constructorArguments)
        {
            var contracts = ctx.Component.Contracts.Where(i => !string.IsNullOrEmpty(i.FullName)).ToArray();

            if (ctx.GenericParameters != null)
            {
                for (int i = 0; i < contracts.Length; i++)
                {
                    if (contracts[i].IsOpenGenericType() && contracts[i].GetGenericArguments().Length == ctx.GenericParameters.Length)
                    {
                        contracts[i] = contracts[i].MakeGenericType(ctx.GenericParameters);
                    }
                }
            }

            var matchMethods = (ctx.Component.ExtendedProperties["methods"] as MethodInfo[]).Select(p => p.Name).ToArray();

            var baseInterfaces = contracts.Where(i => i.IsInterface).ToArray();

            var invocationHandler = new SimpleInvocationHandler(matchMethods);

            var impType = ctx.Component.Implementation;

            if (impType.IsAbstract || impType.IsInterface)
                throw new ActivatorException(
                   string.Format(Mini_Resources.TypeAbstract,
                       impType.FullName,
                       ctx.Component.Contracts[0].FullName));

            if (impType.IsOpenGenericType())
                impType = impType.MakeCloseGenericType(ctx.GenericParameters);


            var proxy = NLite.Reflection.Proxy.NewProxyInstance(impType, baseInterfaces,
               invocationHandler, constructorArguments
               );

            return proxy;
        }