コード例 #1
0
        /// <summary>
        /// 读取
        /// </summary>
        /// <param name="key">键,接口全路径,包含接口的程序集名,比如:Hzdtf.Contract,Hzdtf.Contract.ITestable</param>
        /// <returns>值,接口对应的实现类名,比如:Hzdtf.Impl,Hzdtf.Impl.Test</returns>
        public string Reader(string key)
        {
            if (dicCaches.ContainsKey(key))
            {
                return(dicCaches[key]);
            }
            else
            {
                var dicMap = readerConfig.Reader();
                if (dicMap.IsNullOrCount0())
                {
                    throw new Exception("找不到任何程序集的配置");
                }

                // 获取接口程序集
                string interfaceAssemblyName;
                var    interfaceClassFullName = ReflectUtil.GetClassAndAssemblyFullName(key, out interfaceAssemblyName);
                if (string.IsNullOrWhiteSpace(interfaceAssemblyName))
                {
                    throw new Exception($"[{key}]的接口程序集不能为空");
                }

                var interfaceAssembly = ReflectUtil.GetAssembly(interfaceAssemblyName);
                if (interfaceAssembly == null)
                {
                    throw new Exception($"找不到[{interfaceAssemblyName}]的接口程序集");
                }

                Type interfaceType = interfaceAssembly.GetType(interfaceClassFullName);
                if (interfaceType == null)
                {
                    throw new Exception($"找不到[{interfaceClassFullName}]的接口类型");
                }

                // 找到对应的实现程序集
                if (dicMap.ContainsKey(interfaceAssemblyName))
                {
                    var implAssemblyName = dicMap[interfaceAssemblyName];
                    var implAssembly     = ReflectUtil.GetAssembly(implAssemblyName);
                    if (implAssembly == null)
                    {
                        throw new Exception($"[{interfaceAssemblyName}]的对应的实现程序集");
                    }

                    var implType = implAssembly.GetTypes().Where(t => t.IsClass && t.IsPublic && interfaceType.IsAssignableFrom(t)).FirstOrDefault();
                    if (implType == null)
                    {
                        throw new Exception($"[{interfaceClassFullName}]的对应的实现类");
                    }

                    var implFullName = $"{implAssemblyName},{implType.FullName}";

                    Set(key, implFullName);

                    return(implFullName);
                }
                else
                {
                    throw new Exception($"找不到[{interfaceAssemblyName}]的接口程序集的配置");
                }
            }

            throw new Exception($"找不到[{key}]对应的实现类");
        }