Пример #1
0
        /// <summary> 生成代理类型 </summary>
        /// <param name="baseType"></param>
        /// <param name="interfaceType"></param>
        /// <returns></returns>
        private Type GenerateProxyType(Type baseType, Type interfaceType)
        {
            var baseTypeInfo = baseType.GetTypeInfo();

            if (!interfaceType.GetTypeInfo().IsInterface)
            {
                throw new ArgumentException($"InterfaceType_Must_Be_Interface, {interfaceType.FullName}", nameof(interfaceType));
            }

            if (baseTypeInfo.IsSealed)
            {
                throw new ArgumentException($"BaseType_Cannot_Be_Sealed, {baseTypeInfo.FullName}", nameof(baseType));
            }

            if (baseTypeInfo.IsAbstract)
            {
                throw new ArgumentException($"BaseType_Cannot_Be_Abstract {baseType.FullName}", nameof(baseType));
            }

            var pb = _proxyAssembly.CreateProxy($"Zooyard.Proxy.{interfaceType.Namespace}_{interfaceType.Name}", baseType);

            foreach (var t in interfaceType.GetTypeInfo().ImplementedInterfaces)
            {
                pb.AddInterfaceImpl(t);
            }

            pb.AddInterfaceImpl(interfaceType);

            var generatedProxyType = pb.CreateType();

            return(generatedProxyType);
        }