コード例 #1
0
        public IEnumerable <Type> GenerateProxys(IEnumerable <Type> interfaceTypes)
        {
            var assembles = DependencyContext.Default.RuntimeLibraries
                            .SelectMany(i => i.GetDefaultAssemblyNames(DependencyContext.Default)
                                        .Select(z => Assembly.Load(new AssemblyName(z.Name))));

            assembles = assembles.Where(i => i.IsDynamic == false).ToArray();

            var enumerable = interfaceTypes as Type[] ?? interfaceTypes.ToArray();
            var trees      = enumerable.Select(GenerateProxyTree).ToList();
            var stream     = CompilationUnits.CompileClientProxy(trees,
                                                                 assembles
                                                                 .Select(a => MetadataReference.CreateFromFile(a.Location))
                                                                 .Concat(new[]
            {
                MetadataReference.CreateFromFile(typeof(Task).GetTypeInfo().Assembly.Location)
            }),
                                                                 enumerable.ToArray()[0],
                                                                 _logger);

            if (stream == null)
            {
                throw new ArgumentException(@"没有生成任何客户端代码", nameof(stream));
            }

            using (stream)
            {
                var className = enumerable.ToArray()[0].Name.StartsWith("I")
                    ? enumerable.ToArray()[0].Name.Substring(1)
                    : enumerable.ToArray()[0].Name;
                return(AppDomain.CurrentDomain.GetAssemblies().Any(x => x.FullName.Contains(className))
                    ? Assembly.Load(StreamToBytes(stream)).GetExportedTypes()
                    : AssemblyLoadContext.Default.LoadFromStream(stream).GetExportedTypes());
            }
        }
コード例 #2
0
        /// <summary>
        /// 生成服务代理
        /// </summary>
        /// <param name="interfaceTypes">需要被代理的接口类型</param>
        /// <returns>服务代理实现</returns>
        public IEnumerable <Type> GenerateProxys(IEnumerable <Type> interfaceTypes)
        {
//            var assembles = DependencyContext.Default.RuntimeLibraries
//                .SelectMany(i => i.GetDefaultAssemblyNames(DependencyContext.Default)
//                    .Select(z => Assembly.Load(new AssemblyName(z.Name))));

            var assembles = AppDomain.CurrentDomain.GetAssemblies().AsParallel().Where(i => i.IsDynamic == false).ToArray();

//            assembles = assembles.Where(i => i.IsDynamic == false).ToArray();

            var trees  = interfaceTypes.Select(GenerateProxyTree).ToList();
            var stream = CompilationUnits.CompileClientProxy(trees,
                                                             assembles
                                                             .Select(a => MetadataReference.CreateFromFile(a.Location))
                                                             .Concat(new[]
            {
                MetadataReference.CreateFromFile(typeof(Task).GetTypeInfo().Assembly.Location)
            }),
                                                             _logger);

            if (stream == null)
            {
                throw new ArgumentException("没有生成任何客户端代码", nameof(stream));
            }

            using (stream)
            {
                var assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
                return(assembly.GetExportedTypes());
            }
        }