public void Test_GetDescription() { Assert.Equal("", Reflections.GetDescription <EnumSample>("X")); Assert.Equal("A", Reflections.GetDescription <EnumSample>("A")); Assert.Equal("B2", Reflections.GetDescription <EnumSample>("B")); Assert.Equal("IntValue", Reflections.GetDescription <Sample>("IntValue")); }
/// <summary> /// Bing框架初始化,适用于AspNetCore环境 /// </summary> /// <param name="app">应用程序构建器</param> public static IApplicationBuilder UseBing(this IApplicationBuilder app) { var provider = app.ApplicationServices; var logger = provider.GetLogger(FrameworkLog); logger.LogInformation("Bing框架初始化开始"); // 输出注入服务的日志 //var startupLogger = provider.GetService<StartupLogger>(); //startupLogger.Output(provider); var watch = Stopwatch.StartNew(); var modules = provider.GetAllModules(); foreach (var module in modules) { var moduleName = Reflections.GetDescription(module.GetType()); logger.LogInformation($"正在初始化模块 “{moduleName}”"); if (module is AspNetCoreBingModule netCoreModule) { netCoreModule.UseModule(app); } else { module.UseModule(provider); } logger.LogInformation($"模块 “{moduleName}” 初始化完成"); } watch.Stop(); logger.LogInformation($"Bing框架初始化完成,耗时:{watch.Elapsed}"); return(app); }
/// <summary> /// Add /// </summary> /// <param name="expression"></param> /// <param name="newValue"></param> /// <typeparam name="TObject"></typeparam> /// <typeparam name="TProperty"></typeparam> /// <typeparam name="TValue"></typeparam> public void Add <TObject, TProperty, TValue>(Expression <Func <TObject, TProperty> > expression, TValue newValue) where TObject : IDomainObject { var name = Lambdas.GetName(expression); var desc = Reflections.GetDescription(Lambdas.GetMember(expression)); var value = Lambdas.GetValue(expression); Add(name, desc, value.CastTo <TValue>(), newValue); }
/// <summary> /// 添加模块 /// </summary> /// <param name="type">类型</param> private IBingBuilder AddModule(Type type) { if (!type.IsBaseOn(typeof(BingModule))) { throw new BingFrameworkException($"要加载的模块类型“{type}”不派生于基类 {nameof(BingModule)}"); } if (_modules.Any(m => m.GetType() == type)) { return(this); } var tmpModules = new BingModule[_modules.Count]; _modules.CopyTo(tmpModules); var module = _sourceModules.FirstOrDefault(x => x.GetType() == type); if (module == null) { throw new BingFrameworkException($"类型为“{type.FullName}”的模块实例无法找到"); } _modules.AddIfNotContains(module); // 添加依赖模块 var dependTypes = module.GetDependModuleTypes(); foreach (var dependType in dependTypes) { var dependModule = _sourceModules.Find(m => m.GetType() == dependType); if (dependModule == null) { throw new BingFrameworkException($"加载模块{module.GetType().FullName}时无法找到依赖模块{dependType.FullName}"); } _modules.AddIfNotContains(dependModule); } // 按先层级后顺序的规则进行排序 _modules = _modules.OrderBy(m => m.Level).ThenBy(m => m.Order).ToList(); var logName = typeof(BingBuilder).FullName; tmpModules = _modules.Except(tmpModules).ToArray(); foreach (var tmpModule in tmpModules) { var moduleType = tmpModule.GetType(); var moduleName = Reflections.GetDescription(moduleType); Services.LogInformation($"添加模块 “{moduleName} ({moduleType.Name})” 的服务", logName); var tmp = Services.ToArray(); AddModule(Services, tmpModule); Services.ServiceLogDebug(tmp, moduleType.FullName); Services.LogInformation($"模块 “{moduleName} ({moduleType.Name})” 的服务添加完毕,添加了 {Services.Count - tmp.Length} 个服务\n", logName); } return(this); }
/// <summary> /// 添加描述项 /// </summary> /// <typeparam name="T">枚举类型</typeparam> /// <param name="result">结果集</param> /// <param name="field">枚举字段</param> /// <param name="enumType">枚举类型</param> private static void AddItem <T>(ICollection <Item> result, FieldInfo field, Type enumType) { if (!field.FieldType.IsEnum) { return; } var value = GetValue <T>(field, enumType); var description = Reflections.GetDescription(field); var sortId = GetSortId(field); if (sortId == -1) { sortId = value; } result.Add(new Item(description, value.ToString(), sortId)); }
/// <summary> /// Bing模块初始化,适用于非AspNetCore环境 /// </summary> /// <param name="serviceProvider">服务提供程序</param> public static IServiceProvider UseBing(this IServiceProvider serviceProvider) { var logger = serviceProvider.GetLogger(FrameworkLog); logger.LogInformation("Bing框架初始化开始"); var watch = Stopwatch.StartNew(); var modules = serviceProvider.GetServices <BingModule>().ToArray(); foreach (var module in modules) { var moduleName = Reflections.GetDescription(module.GetType()); logger.LogInformation($"正在初始化模块 “{moduleName}”"); module.UseModule(serviceProvider); logger.LogInformation($"模块 “{moduleName}” 初始化完成"); } watch.Stop(); logger.LogInformation($"Bing框架初始化完毕,耗时:{watch.Elapsed}"); return(serviceProvider); }
/// <summary> /// 获取描述,使用System.ComponentModel.Description特性设置描述 /// </summary> /// <param name="type">枚举类型</param> /// <param name="member">成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0</param> /// <returns></returns> public static string GetDescription(Type type, object member) { return(Reflections.GetDescription(type, GetName(type, member))); }
/// <summary> /// 获取描述,使用System.ComponentModel.Description特性设置描述 /// </summary> /// <typeparam name="T">枚举</typeparam> /// <param name="member">成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0</param> /// <returns></returns> public static string GetDescription <T>(object member) { return(Reflections.GetDescription <T>(GetName <T>(member))); }