예제 #1
0
 // 获取所有assembly
 public static List <Assembly> GetAllAssemblys()
 {
     if (s_asms == null)
     {
         s_asms = new List <Assembly> ();
         var _files = PathMethods.GetAllAssemblyFileNames();
         foreach (var _file in _files)
         {
             try {
                 var _asm = Assembly.LoadFrom(_file);
                 if (!s_asms.Contains(_asm))
                 {
                     s_asms.Add(_asm);
                 }
             } catch (Exception) {
             }
         }
     }
     return(s_asms);
 }
예제 #2
0
        // 初始化接口信息
        public static (List <string>, List <string>) InitInterfaces()
        {
            List <string> _local = new List <string> (), _remote = new List <string> ();

            // 枚举所有接口
            var _types = PathMethods.GetAllTypes();

            foreach (var _type in _types)
            {
                var _type_attr = _type.GetCustomAttribute <SimpleMSServiceAttribute> ();
                if (_type_attr == null)
                {
                    continue;
                }
                if (!_type.IsInterface)
                {
                    throw new TypeLoadException("具有 [SimpleMSService] 标注的类必须为接口类型");
                }

                // 获取接口服务名称
                string _name         = _type.FullName.Replace('.', '_');
                string _service_name = _type.GetServiceName();

                // 如果需要,那么搜索本地模块
                var _type_impls = (from p in _types where p.GetInterface(_type.Name) == _type select p);
                if (_type_impls.Count() > 1)
                {
                    throw new TypeLoadException("实现 [SimpleMSService] 标注的接口的类最多只能有一个");
                }
                object _impl_o = null;
                if (_type_impls.Any())
                {
                    // 从本地模块加载
                    _local.Add(_service_name);

                    // 创建实例
                    _impl_o = Activator.CreateInstance(_type_impls.First());
                }
                else
                {
                    // 从外部模块加载
                    _remote.Add(_service_name);

                    // 创建类生成器
                    var _assembly_name    = new AssemblyName($"_faw_assembly__{_name}_");
                    var _assembly_builder = AssemblyBuilder.DefineDynamicAssembly(_assembly_name, AssemblyBuilderAccess.Run);
                    var _module_builder   = _assembly_builder.DefineDynamicModule($"_faw_module__{_name}_");
                    var _type_builder     = _module_builder.DefineType($"_faw_type__{_name}_", TypeAttributes.Public | TypeAttributes.Class);

                    // 创建构造函数
                    var _constructor_builder = _type_builder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Array.Empty <Type> ());
                    _constructor_builder.GetILGenerator().Emit(OpCodes.Ret);

                    // 定义存储降级函数的字段
                    var _deg_funcs = new List <Func <Dictionary <string, object>, Type, Exception, Task> > ();
                    var _deg_field = _type_builder.DefineField("_faw_field__deg_funcs_", typeof(List <Func <Dictionary <string, object>, Type, Exception, Task> >), FieldAttributes.Public);

                    // 定义存储返回类型的字段
                    var _return_types = new List <Type> ();
                    var _return_field = _type_builder.DefineField("_faw_field__return_types_", typeof(List <Type>), FieldAttributes.Public);

                    // 循环新增新的函数处理
                    _type_builder.AddInterfaceImplementation(_type);
                    var _method_infos = _type.GetMethods();
                    for (int i = 0; i < _method_infos.Length; ++i)
                    {
                        // 不允许出现getter和setter
                        if (_method_infos [i].Name.StartsWith("get_") || _method_infos [i].Name.StartsWith("set_"))
                        {
                            throw new TypeLoadException("具有 [SimpleMSService] 标注的接口不允许出现 getter/setter 函数 (函数名不允许 get_/set_ 开头)");
                        }

                        // 只提供异步函数
                        if (_method_infos [i].ReturnType != typeof(Task) && _method_infos [i].ReturnType.BaseType != typeof(Task))
                        {
                            throw new TypeLoadException($"具有 [SimpleMSService] 标注的接口函数 {_method_infos [i].Name} 返回类型非 Task");
                        }

                        // 将回调函数与返回类型函数
                        var _deg_func = _method_infos [i].GetCustomAttribute <MethodDegradationAttribute> ()?.DegradationFunc;
                        _deg_funcs.Add(_deg_func);
                        _return_types.Add(_method_infos [i].ReturnType);
                        _add_transcall_method(_service_name, _type_builder, _method_infos [i], _deg_field, _return_field, i);
                    }

                    // 创建实例
                    var _impl_type = _type_builder.CreateType();
                    _impl_o = Activator.CreateInstance(_impl_type);
                    _impl_type.InvokeMember("_faw_field__deg_funcs_", BindingFlags.SetField, null, _impl_o, new [] { _deg_funcs });
                    _impl_type.InvokeMember("_faw_field__return_types_", BindingFlags.SetField, null, _impl_o, new [] { _return_types });
                }

                // 添加进处理对象
                Singletons.Option.ServicesCollection.AddSingleton(_type, _impl_o);
                Singletons.CallerMap.Add((_service_name, _type), _impl_o);
            }

            return(_local, _remote);
        }