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 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); }