public void SetUp() { objectRepository = new Dictionary <string, object>(); ReflectionOptimizer.Enable(); var apiCtx = BuildRoutine.Context() .AsClientApplication( codingStyle = BuildRoutine.CodingStyle() .FromBasic() .AddCommonSystemTypes() .AddTypes(GetType().Assembly, t => t.Namespace?.StartsWith("Routine.Test.Performance.Domain") == true) .Use(p => p.ParseableValueTypePattern()) .Initializers.Add(c => c.PublicConstructors().When(type.of <BusinessPerformanceInput>())) .Datas.Add(c => c.Properties(m => !m.IsInherited(true, true))) .DataFetchedEagerly.Set(true) .Operations.Add(c => c.Methods(o => !o.IsInherited(true, true))) .IdExtractor.Set(c => c.IdByProperty(m => m.Returns <int>("Id"))) .Locator.Set(c => c.Locator(l => l.SingleBy(id => objectRepository[id]))) .ValueExtractor.Set(c => c.Value(e => e.By(o => $"{o}"))) ); objectService = apiCtx.ObjectService; rapp = apiCtx.Application; var _ = objectService.ApplicationModel; }
/// <summary> /// 启动主服务 /// </summary> /// <param name="service"></param> private void ServiceExecute(string service) { IServiceFactory sf = ReflectionOptimizer.CreateInstanceMethod(service)() as IServiceFactory; if (sf != null) { sf.ServiceInit(); } }
public void When_disabled__it_does_not_optimize() { ReflectionOptimizer.Disable(); var invoker = InvokerFor <OptimizedClass>("VoidMethod") as ProxyMethodInvoker; Assert.IsNotNull(invoker); Assert.IsInstanceOf <ReflectionMethodInvoker>(invoker.Real); }
public override void SetUp() { base.SetUp(); ReflectionOptimizer.Enable(); mock = new Mock <IOptimizedInterface <string> >(); target = new OptimizedClass(mock.Object); }
private void FormatStrAttr(PlatFormInfo platform, AuthenticationInfo auth, AuthNode node, string attrName) { var attrValue = ReflectionOptimizer.GetProperty <AuthenticationInfo, string>(auth, attrName); if (string.IsNullOrEmpty(attrValue)) { attrValue = ReflectionOptimizer.GetProperty <PlatFormInfo, string>(platform, attrName); } ReflectionOptimizer.SetProperty <AuthNode, string>(node, attrValue, attrName); }
/// <summary> /// 启动扩展服务 /// </summary> /// <param name="extensions"></param> private void ExtensionExecute(IEnumerable <string> extensions) { foreach (string extension in extensions) { IExtension ex = ReflectionOptimizer.CreateInstanceMethod(extension)() as IExtension; if (ex != null) { ex.Execute(); } } }
private void FormatBoolAttr(PlatFormInfo platform, AuthNode node, string attrName) { bool value; if (!string.IsNullOrEmpty(node.PlatformName) && AppSettingCache.Instance.ContainerKey(node.PlatformName) && string.Compare(attrName, AppSettingCache.Instance[node.PlatformName]) == 0) { value = true; } else { var attrValue = ReflectionOptimizer.GetProperty <PlatFormInfo, string>(platform, attrName); value = Convert.ToBoolean(int.Parse(attrValue)); } ReflectionOptimizer.SetProperty <AuthNode, bool>(node, value, attrName); }
public void TestGetterTypeMismatch() { var obj = new GetterTypeMismatchClass(); const string property = "Property"; NoSetterAccessor accessor = new NoSetterAccessor(new CamelCaseUnderscoreStrategy()); Assert.IsTrue(accessor.CanAccessThroughReflectionOptimizer); ReflectionOptimizer reflectionOptimizer = new ReflectionOptimizer( obj.GetType(), new[] { accessor.GetGetter(obj.GetType(), property) }, new[] { accessor.GetSetter(obj.GetType(), property) }); IAccessOptimizer accessOptimizer = reflectionOptimizer.AccessOptimizer; accessOptimizer.SetPropertyValues(obj, new object[] { 10 }); object[] values = accessOptimizer.GetPropertyValues(obj); Assert.AreEqual("str", values[0]); }
public ServiceTypeField(ServiceAssembly assembly, ReflectionOptimizer.Getter getter, ReflectionOptimizer.Setter setter, ReflectionOptimizer.ShouldSerializeInvoker shouldSerializeMethod, int tag, bool isRequired, Type type) { Require.NotNull(getter, "getter"); Require.NotNull(setter, "setter"); Require.NotNull(type, "type"); Getter = getter; Setter = setter; ShouldSerializeMethod = shouldSerializeMethod; Tag = tag; IsRequired = isRequired; Type = type; // See whether the field is a collection type. var listItemType = TypeUtil.GetCollectionType(type); if (listItemType != null) { // We handle two collection types: List<> and the rest. // The rest we handle as an array. bool isList = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>); if (isList) CollectionType = type; else CollectionType = listItemType.MakeArrayType(); Type = listItemType; } // Build the service type if the field type is a protobuf type. var typeAttributes = Type.GetCustomAttributes(typeof(ProtoContractAttribute), true); if (typeAttributes.Length > 0) ServiceType = assembly.GetServiceType(Type); }
public void ReflectionCore_PropertyAccess() { const int load = 100000; Console.WriteLine("Load -> " + load); Console.WriteLine("-------"); var obj = new BusinessPerformance { Id = 1 }; Run("Direct Access", () => { var _ = obj.Id; }, load); Console.WriteLine("-------"); var prop = obj.GetType().GetProperty("Id"); Run("System.Reflection Cached Access", () => { var _ = prop?.GetValue(obj, Array.Empty <object>()); }, load); ReflectionOptimizer.Disable(); var rprop1 = obj.GetTypeInfo().GetProperty("Id"); Run("Routine.Core.Reflection Cached Access (without optimizer) ", () => { var _ = rprop1.GetValue(obj); }, load); ReflectionOptimizer.Enable(); var rprop2 = obj.GetTypeInfo().GetProperty("Id"); Run("Routine.Core.Reflection Cached Access", () => { var _ = rprop2.GetValue(obj); }, load); Console.WriteLine("-------"); Run("System.Reflection Access", () => { var _ = obj.GetType().GetProperty("Id")?.GetValue(obj, Array.Empty <object>()); }, load); ReflectionOptimizer.Disable(); Run("Routine.Core.Reflection Access (without optimizer)", () => { var _ = obj.GetTypeInfo().GetProperty("Id").GetValue(obj); }, load); ReflectionOptimizer.Enable(); Run("Routine.Core.Reflection Access", () => { var _ = obj.GetTypeInfo().GetProperty("Id").GetValue(obj); }, load); Console.WriteLine("-------"); Run("Routine.Core.Reflection Access -> GetTypeInfo()", () => { var _ = obj.GetTypeInfo(); }, load); var type = obj.GetTypeInfo(); Run("Routine.Core.Reflection Access -> GetProperty('Id')", () => { var _ = type.GetProperty("Id"); }, load); var rprop3 = type.GetProperty("Id"); Run("Routine.Core.Reflection Access -> GetValue(obj)", () => { var _ = rprop3.GetValue(obj); }, load); }
public object ServiceInit() { string configPath = string.Format("{0}/Config/ConsoleCommand.xml", AppDomain.CurrentDomain.BaseDirectory); if (!File.Exists(configPath)) { return(null); } var root = XElement.Load(configPath); var cmds = from data in root.Elements("Command") select data; foreach (var cmd in cmds) { IConsoleCommand consoleCmd = ReflectionOptimizer.CreateInstanceMethod((string)cmd.Attribute("Type"))() as IConsoleCommand; if (consoleCmd != null) { string key = (string)cmd.Attribute("Key"); consoleCmd.SetKey(key); if (!_cmds.ContainsKey(key)) { _cmds.Add(key, consoleCmd); } } } bool flag = true; while (flag) { var readStr = Console.ReadLine().Split(_split, StringSplitOptions.RemoveEmptyEntries); if (readStr.Length > 0) { if (_cmds.ContainsKey(readStr[0])) { IConsoleCommand consoleCmd = _cmds[readStr[0]]; if (!consoleCmd.IsStarting) { ThreadPool.QueueUserWorkItem(state => { try { Console.WriteLine("{0} {1}", DateTime.Now, consoleCmd.StartLine); consoleCmd.Fun(readStr); Console.WriteLine("{0} {1}", DateTime.Now, consoleCmd.CompleteLine); } catch (Exception ex) { Console.WriteLine(ex); } finally { consoleCmd.IsStarting = false; } }); consoleCmd.IsStarting = true; } else if (consoleCmd.IsStarting) { Console.WriteLine(consoleCmd.RunningLine); } } else if (readStr[0] == "exit") { flag = false; } else { Console.WriteLine("Bad Command,Please Input Again!"); } } else { Console.WriteLine("Bad Command,Please Input Again!"); } } return(null); }
public static IApplicationBuilder UseRoutineInProductionMode(this IApplicationBuilder source) { ReflectionOptimizer.Enable(); return(source); }
public static IApplicationBuilder UseRoutineInDevelopmentMode(this IApplicationBuilder source) { ReflectionOptimizer.Disable(); return(source); }
private void FormatStrAttr(PlatFormInfo platform, AuthNode node, string attrName) { var attrValue = ReflectionOptimizer.GetProperty <PlatFormInfo, string>(platform, attrName); ReflectionOptimizer.SetProperty <AuthNode, string>(node, attrValue, attrName); }