/// <summary> /// 动态调用无参数方法 /// </summary> /// <param name="service">服务名称,区分大小写:比如UserService,TypeService</param> /// <param name="method">无参数方法名称:</param> /// <returns></returns> public static T Eval <T>(string service, string method) { var serviceResolve = DynamicService.Resolve(service); if (serviceResolve == null) { throw new ValidException("服务类型输入错误,未找到"); } var target = new Interpreter().SetVariable("entityService", serviceResolve); var evalString = $"entityService.{method}()"; var result = target.Eval(evalString); return((T)result); }
/// <summary> /// Gets the single user. /// 动态获取用户 /// </summary> /// <param name="userName">Name of the user.</param> /// <returns>dynamic.</returns> public static dynamic GetSingleUser(string userName) { var objectCache = Ioc.Resolve <IObjectCache>(); return(objectCache.GetOrSet(() => { var userService = DynamicService.Resolve("User"); var target = new Interpreter().SetVariable("userService", userService); var parameters = new[] { new Parameter("userName", userName) }; var user = (dynamic)target.Eval("userService.GetSingle(userName)", parameters); return user; }, "dynamic_GetSingleUser" + userName, TimeSpan.FromHours(1)).Value); }
/// <summary> /// 动态调用一个参数的方法 /// </summary> /// <param name="service">服务名称,区分大小写:比如UserService,TypeService</param> /// <param name="method">方法名称:</param> /// <param name="parameter">参数</param> /// <returns></returns> public static void Eval(string service, string method, object parameter) { var serviceResolve = DynamicService.Resolve(service); if (serviceResolve == null) { throw new ValidException("服务类型输入错误,未找到"); } var parameters = new[] { new Parameter("parameter", parameter) }; var target = new Interpreter().SetVariable("entityService", serviceResolve); var evalString = $"entityService.{method}(parameter)"; target.Eval(evalString, parameters); }
/// <summary> /// Gets the user grade. /// 动态获取用户等级 /// </summary> /// <param name="gradeId">The grade identifier.</param> /// <returns>dynamic.</returns> public static dynamic GetUserGrade(Guid gradeId) { if (gradeId.IsGuidNullOrEmpty()) { return(null); } // 获取等级 var gardeService = DynamicService.Resolve("GradeService"); var gradeTarget = new Interpreter().SetVariable("gardeService", gardeService); // 获取等级 var parameters = new[] { new Parameter("gradeId", gradeId) }; var gradeConfig = (dynamic)gradeTarget.Eval("gardeService.GetGrade(gradeId)", parameters); return(gradeConfig); }