private static void MapSignalWithNoParameters( IView view, BaseSignal signal, string baseName) { var type = view.GetType(); var methodName = "On" + baseName; var method = type.GetMethods(BINDING_FLAGS) .Where(x => x.GetParameters().Length == 0) .FirstOrDefault(x => x.Name.Equals(methodName)); if (method != null) { var signalMapping = view.SignalMappings.FirstOrDefault(x => x.Name.Equals(baseName)); if (signalMapping == null) { var mappingType = typeof(SignalMap); signalMapping = (ISignalMapping)Activator.CreateInstance(mappingType, baseName, signal); view.SignalMappings.Add(signalMapping); } var actionType = typeof(Action); var action = (Action)Delegate.CreateDelegate(actionType, view, method); var command = new ActionCommand(action); ReflectionCommon.InvokeMethod(signalMapping, "RegisterCommand", new object[] { command }); } }
private static void MapSignalWithTwoParameters( IView view, BaseSignal signal, string baseName) { var type = view.GetType(); var signalType = GetSignalType(signal); var methodName = "On" + baseName; var method = type.GetMethods(BINDING_FLAGS) .Where(x => x.GetParameters().Length == 2) .Where(x => x.GetParameters()[0].ParameterType == signalType.GetGenericArguments()[0] && x.GetParameters()[1].ParameterType == signalType.GetGenericArguments()[1]) .FirstOrDefault(x => x.Name.Equals(methodName)); if (method != null) { var signalMapping = view.SignalMappings.FirstOrDefault(x => x.Name.Equals(baseName)); if (signalMapping == null) { var mappingType = typeof(SignalMap <,>).MakeGenericType(signalType.GetGenericArguments()); signalMapping = (ISignalMapping)Activator.CreateInstance(mappingType, baseName, signal); view.SignalMappings.Add(signalMapping); } var actionType = typeof(Action <,>).MakeGenericType(signalType.GetGenericArguments()); var action = Delegate.CreateDelegate(actionType, view, method); var commandType = typeof(ActionCommand <,>).MakeGenericType(signalType.GetGenericArguments()); var command = Activator.CreateInstance(commandType, action); ReflectionCommon.InvokeMethod(signalMapping, "RegisterCommand", new[] { command }); } }
public List <T> GetItemListByConditions <T>(string tableName, List <T> list, List <FilterCondition> filter) { List <T> result = new List <T>(); List <T> subList = null; if (filter == null || list == null || list.Count == 0) { return(result); } for (int i = 0; i < filter.Count; i++) { FilterCondition cond = filter[i]; if (cond.Value != null) { cond.CompareType = !cond.Value.ToString().Contains("*") ? TableCompareType.EQ : TableCompareType.TEXT; if (cond.CompareType == TableCompareType.EQ) { result = result.Concat(list.FindAll(d => { object val = ReflectionCommon.GetValue(d, cond.Key); if (val == null) { return(false); } return(cond.Value.ToString() == val.ToString()); })).ToList(); } else { subList = GetItemListBySearchStr(list, cond); if (subList.Count == 0) { result.Clear(); break; } result = result.Concat(subList).ToList(); } } else { result = list; } } return(result); }
public List <T> OrderItemList <T>(List <T> list, List <FilterCondition> conds) { IOrderedEnumerable <T> olist = null; if (conds == null || conds.Count == 0) { return(list); } olist = list.OrderBy(d => ReflectionCommon.GetValue(d, conds[0].Key)); foreach (FilterCondition c in conds) { olist = OrderItemList <T>(olist, c); } return(olist.ToList()); }
public List <T> GetItemListBySearchStr <T>(List <T> list, FilterCondition condition) { if (list == null) { return(new List <T>()); } return(list.FindAll(d => { object val = ReflectionCommon.GetValue(d, condition.Key); if (val == null) { return false; } return condition.GetRegexValue().IsMatch(val.ToString()); })); }
public bool UpdateItem <T>(string tableName, FilterCondition cond, T data, string[] columns) { IMongoCollection <T> collection = db.GetCollection <T>(tableName); FilterDefinition <T> filter = Builders <T> .Filter.Eq(cond.Key, cond.Value); UpdateDefinition <T> update = null; if (columns == null) { return(false); } foreach (string col in columns) { update = Builders <T> .Update.AddToSet(col, ReflectionCommon.GetValue <T>(data, col)); } return(1 == (int)collection.UpdateOne(transaction, filter, update).ModifiedCount); }
public void InvokeMethod_Test() { var n = 5; var s = "test"; var c = string.Format("{0}{1}", s, n); var testClass = new TestClass(); // generic static var resultA = (int)ReflectionCommon.InvokeGenericStaticMethod(typeof(TestClass), "TestStaticGenericMethodA", n, typeof(int)); Assert.IsTrue(resultA == n); resultA = (int)ReflectionCommon.InvokeGenericStaticMethod <TestClass>("TestStaticGenericMethodA", n, typeof(int)); Assert.IsTrue(resultA == n); var resultB = (string)ReflectionCommon.InvokeGenericStaticMethod(typeof(TestClass), "TestStaticGenericMethodB", new object[] { s, n }, typeof(string), typeof(int)); Assert.IsTrue(resultB == c); resultB = (string)ReflectionCommon.InvokeGenericStaticMethod <TestClass>("TestStaticGenericMethodB", new object[] { s, n }, typeof(string), typeof(int)); Assert.IsTrue(resultB == c); // generic instance resultA = (int)ReflectionCommon.InvokeGenericMethod(testClass, "TestGenericMethodA", n, typeof(int)); Assert.IsTrue(resultA == n); resultB = (string)ReflectionCommon.InvokeGenericMethod(testClass, "TestGenericMethodB", new object[] { s, n }, typeof(string), typeof(int)); Assert.IsTrue(resultB == c); // static non-generic resultA = (int)ReflectionCommon.InvokeStaticMethod(typeof(TestClass), "TestStaticMethodA", n); Assert.IsTrue(resultA == n); resultA = (int)ReflectionCommon.InvokeStaticMethod <TestClass>("TestStaticMethodA", n); Assert.IsTrue(resultA == n); resultB = (string)ReflectionCommon.InvokeStaticMethod(typeof(TestClass), "TestStaticMethodB", new object[] { s, n }); Assert.IsTrue(resultB == c); resultB = (string)ReflectionCommon.InvokeStaticMethod <TestClass>("TestStaticMethodB", new object[] { s, n }); Assert.IsTrue(resultB == c); // instance non-generic resultA = (int)ReflectionCommon.InvokeMethod(testClass, "TestMethodA", n); Assert.IsTrue(resultA == n); resultB = (string)ReflectionCommon.InvokeMethod(testClass, "TestMethodB", new object[] { s, n }); Assert.IsTrue(resultB == c); }
public void FindAllRelatedTypes_Test() { var relatedTypes = typeof(TestClass).FindAllDerivedTypes().ToList(); Assert.IsTrue(relatedTypes.Count == 3); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedA))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedB))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedC))); relatedTypes = typeof(TestClass).FindAllDerivedTypes(null, true).ToList(); Assert.IsTrue(relatedTypes.Count == 4); Assert.IsTrue(relatedTypes.Contains(typeof(TestClass))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedA))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedB))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedC))); relatedTypes = typeof(TestClass).FindAllDerivedTypes(null, true, true).ToList(); Assert.IsTrue(relatedTypes.Count == 5); Assert.IsTrue(relatedTypes.Contains(typeof(TestClass))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedA))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedB))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedC))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedD))); relatedTypes = ReflectionCommon.FindAllDerivedTypes <TestClass>().ToList(); Assert.IsTrue(relatedTypes.Count == 3); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedA))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedB))); Assert.IsTrue(relatedTypes.Contains(typeof(TestClassDerivedC))); relatedTypes = ReflectionCommon.FindAllDerivedTypes <ITestInterface>().ToList(); Assert.IsTrue(relatedTypes.Count == 0); relatedTypes = ReflectionCommon.FindAllDerivedTypes <ITestInterface>(null, true).ToList(); Assert.IsTrue(relatedTypes.Count == 0); relatedTypes = ReflectionCommon.FindAllDerivedTypes <ITestInterface>(null, true, true).ToList(); Assert.IsTrue(relatedTypes.Count == 2); Assert.IsTrue(relatedTypes.Contains(typeof(ITestInterface))); Assert.IsTrue(relatedTypes.Contains(typeof(ITestInterfaceDerived))); }
public void IsTypeOrDerived_Test() { var testClass = new TestClass(); var testClassA = new TestClassDerivedA(); var testClassB = new TestClassDerivedB(); var isTypeOrDerived = typeof(TestClass).IsTypeOrDerived(typeof(TestClass)); Assert.IsTrue(isTypeOrDerived); isTypeOrDerived = typeof(TestClassDerivedA).IsTypeOrDerived(typeof(TestClass)); Assert.IsTrue(isTypeOrDerived); isTypeOrDerived = typeof(TestClassDerivedC).IsTypeOrDerived(typeof(TestClass)); Assert.IsTrue(isTypeOrDerived); isTypeOrDerived = typeof(TestClassDerivedB).IsTypeOrDerived(typeof(TestClassDerivedA)); Assert.IsFalse(isTypeOrDerived); isTypeOrDerived = typeof(TestClass).IsTypeOrDerived(typeof(TestClassDerivedA)); Assert.IsFalse(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(testClass, testClass); Assert.IsTrue(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(testClass, testClassA); Assert.IsTrue(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(testClassA, testClass); Assert.IsFalse(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(testClassB, testClassA); Assert.IsFalse(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(typeof(TestClass), testClass); Assert.IsTrue(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(typeof(TestClass), testClassA); Assert.IsTrue(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(typeof(TestClassDerivedA), testClass); Assert.IsFalse(isTypeOrDerived); isTypeOrDerived = ReflectionCommon.IsTypeOrDerived(typeof(TestClassDerivedB), testClassA); Assert.IsFalse(isTypeOrDerived); }
public IOrderedEnumerable <T> OrderItemList <T>(IOrderedEnumerable <T> list, FilterCondition cond) { return(list.ThenBy(u => ReflectionCommon.GetValue(u, cond.Key))); }
public static DbContext GetDbContext(this IQueryable self) => ReflectionCommon.GetCurrentDbContextByQueryable(self);