Пример #1
0
 internal Choice(CodeGeneratorOption option)
 {
     option.ApplyTo(this);
 }
        /// <summary>
        /// 为IHttpService创建服务实现
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection BuildProxy(this IServiceCollection services, CodeGeneratorOption option = default)
        {
            var configuration = services.ServiceConfigure();
            var types         = new Collection <Type>();

            foreach (var config in configuration)
            {
                foreach (var type in config.ForTypes)
                {
                    if (!type.IsInterface)
                    {
                        continue;
                    }
                    if (typeof(IHttpService).IsAssignableFrom(type) ||
                        (type.GetCustomAttribute <HttpServiceAttribute>()?.GenerateProxy ?? false))
                    {
                        types.Add(type);
                    }
                }
            }
            var assembly = ProxyGenerator.Generate(types.ToArray(), option);

            foreach (var type in types)
            {
                var proxyType = assembly.ExportedTypes.First(x => type.IsAssignableFrom(x));
                services.AddTransient(type, proxyType);
            }
            return(services);
        }