Exemplo n.º 1
0
        /// <summary>
        /// Creates a proxy for a target type that implelents
        /// an interface type.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="interfaceType"></param>
        /// <returns></returns>
        public Type CreateProxy(Type target, Type interfaceType)
        {
            ProxyGenerator proxyGen = new ProxyGenerator();
            DynamicType proxyType = new DynamicType();

            proxyType.DefineType(target, interfaceType);

            foreach (var method in target.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static))
            {
                if (interfaceType.GetMethod(method.Name, method.GetParameters().Select(x => x.ParameterType).ToArray()) != null)
                {
                    proxyType.OpenMethodForCreation(method);
                    proxyType.CreateMethod();
                }
            }

            Type proxy = proxyType.CreateType();
            return proxy;
        }