public void ExceptionIsThrownWhenSomeLowMeasurementsActuallyIndicateBrighterLightThanTheHighMeasurements() { //Given const byte low = 127; const byte high = byte.MaxValue; const string expectedExceptionMessage = "2 out of 4 measurements from low where equal or higher than the high measurement."; var measurementsHigh = new[, ] { { 20, 5 }, { 10, 45 } }; var measurementsLow = new[, ] { { 20, 50 }, { 5, 40 } }; var measurementGridProcessor = GetMeasurementGridProcessor(); //When var action = new Action(() => { measurementGridProcessor.CreateMinMaxMeasurementGrid(low, high, measurementsHigh, measurementsLow); }); //Then AssertExt.ThrowsException <LowHigherThanHighMeasurementException>(action, expectedExceptionMessage); }
public void ExceptionIsThrownWhenLowAndHighMeasurementsAreSwapped() { //Given const byte low = 127; const byte high = byte.MaxValue; const string expectedExceptionMessage = "All measurements from low are equal or higher than the high measurements."; var measurementsHigh = new[, ] { { 2, 5 }, { 10, 45 } }; var measurementsLow = new[, ] { { 20, 50 }, { 100, 90 } }; var measurementGridProcessor = GetMeasurementGridProcessor(); //When var action = new Action(() => { measurementGridProcessor.CreateMinMaxMeasurementGrid(low, high, measurementsHigh, measurementsLow); }); //Then AssertExt.ThrowsException <LowHighMeasurementsSwappedException>(action, expectedExceptionMessage); }
public void TryingToReturnCenterItemsThrowsExceptionOnNull() { //Given const string expectedExceptionMessage = "An initialized 2D array should have been provided, but null was instead."; //When var action = new Action(() => { Math.GetCenterItems <int[, ]>(null); }); //Then AssertExt.ThrowsException <ArgumentNullException>(action, expectedExceptionMessage); }
public void TryingToReturnCenterItemsThrowsExceptionOnEmptyArray() { //Given const string expectedExceptionMessage = "No items where present in the given 2D array."; var array = new int[0, 4]; //When var action = new Action(() => { array.GetCenterItems(); }); //Then AssertExt.ThrowsException <ArgumentException>(action, expectedExceptionMessage); }
public void ThrowsExceptionIfFileNotFound() { //Given const string expectedExceptionMessage = "File could not be found."; var genericLoader = GetGenericLoader <object>(); //When var action = new Action(() => { genericLoader.GetFromCsvFile(""); }); //Then AssertExt.ThrowsException <FileNotFoundException>(action, expectedExceptionMessage); }
public void ThrowsExceptionWhenTryingToConvertInvalidImageFileTypeToImageFormat() { //Given const string expectedExceptionMessage = ""; const ImageFileType invalidImageFileType = (ImageFileType)int.MaxValue; //When var action = new Action(() => { invalidImageFileType.ToImageFormat(); }); //Then AssertExt.ThrowsException <ArgumentOutOfRangeException>(action, expectedExceptionMessage); }
public void ThrowsExceptionWhenStringCouldNotBeParsedToImageFileType() { //Given var invalidImageFileTypeString = "test"; var expectedExceptionMessage = $"Could not parse string value '{invalidImageFileTypeString}' to a value of '{typeof (ImageFileType).FullName}'"; //When var action = new Action(() => { ImageFileTypeHelper.FromString(invalidImageFileTypeString); }); //Then AssertExt.ThrowsException <InvalidOperationException>(action, expectedExceptionMessage); }
public void ConstructionThrowsOnNullArgument() { //Given IEnumerable <Measurement> measurements = null; //When var action = new Action(() => { // ReSharper disable once ObjectCreationAsStatement // ReSharper disable once ExpressionIsAlwaysNull new MaskIntensityResistanceInterpolator(measurements); }); //Then AssertExt.ThrowsException <ArgumentNullException>(action, ""); }
public void ExceptionThrownInErrorReportIsHidden() { //Given const string exceptionMessage = "Exception to test"; var exceptionToThrow = new Exception(exceptionMessage); var items = GetFullyMockedMain(); var mocks = items.Item1; var main = items.Item2; var errorReportCreatorMock = mocks.ErrorReportCreator; errorReportCreatorMock.Setup( ercm => ercm.CreateReport( It.IsAny <Version>(), It.IsAny <Exception>(), It.IsAny <ApplicationArguments>(), It.IsAny <string>(), It.IsAny <string>())) .Throws <Exception>(); mocks.BitmapProcessor .Setup(bp => bp.Save(It.IsAny <Bitmap>(), It.IsAny <string>(), It.IsAny <ImageFormat>())) .Throws(exceptionToThrow); var applicationArguments = GetApplicationArguments(); //When var action = new Action(() => { main.CreateMask(applicationArguments); }); //Then AssertExt.ThrowsException <Exception>(action, exceptionMessage); errorReportCreatorMock.Verify(erc => erc.CreateReport( It.IsAny <Version>(), It.IsAny <Exception>(), It.IsAny <ApplicationArguments>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once); }
public void ThrowsExceptionWhenDataIsOfUnexpectedType() { //Given var expectedExceptionMessage = $"Expected data of type {typeof(int).FullName} at zero based row:column index 0:0"; var genericGridLoader = GetGridLoader <int>(); const int rows = 1; const int columns = 5; var filePath = StorageManager.GetFullFilePath("stringgrid.csv"); //When var action = new Action(() => { genericGridLoader.GetFromCsvFile(filePath, rows, columns); }); //Then AssertExt.ThrowsException <InvalidDataException>(action, expectedExceptionMessage); }
public void ThrowsExceptionWhenCsvDoesNotContainEnoughRows() { //Given const string expectedExceptionMessage = "Excepted a row at zero based index 0."; var genericGridLoader = GetGridLoader <int>(); const int rows = 1; const int columns = 5; var filePath = StorageManager.GetFullFilePath("empty.csv"); //When var action = new Action(() => { genericGridLoader.GetFromCsvFile(filePath, rows, columns); }); //Then AssertExt.ThrowsException <InvalidDataException>(action, expectedExceptionMessage); }
public void ExceptionIsThrownWhenLowAndHighGridsAreOfDifferentDimensions(int nrOfRowsLow, int nrOfColumnsLow, int nrOfRowsHigh, int nrOfColumnsHigh) { //Given const string expectedExceptionMessage = "The dimensions of the given resistance 2D arrays do not match."; const byte low = 127; const byte high = byte.MaxValue; var lowGrid = new int[nrOfRowsLow, nrOfColumnsLow]; var highGrid = new int[nrOfRowsHigh, nrOfColumnsHigh]; var measurementGridProcessor = GetMeasurementGridProcessor(); //When var action = new Action(() => { measurementGridProcessor.CreateMinMaxMeasurementGrid(low, high, lowGrid, highGrid); }); //Then AssertExt.ThrowsException <InvalidOperationException>(action, expectedExceptionMessage); }
public void LocalMaskIntensityCalculationThrowsExceptionWhenDesiredResistanceValueIsOutOfRangeOfLoadedMeasurements(UseMinOrMax minOrMax) { //Given var measurements = GetDefeaultSetOfMeasurements(); var maskIntensityInterpolator = GetMaskIntensityInterpolator(measurements); var relativePointMinMax = new MinMax <Measurement> { Min = new Measurement(192, 6867), Max = new Measurement(byte.MaxValue, 4623) }; var minResistance = measurements.Min(m => m.Resistance); var maxResistance = measurements.Max(m => m.Resistance); int desiredLocalResistance; switch (minOrMax) { case UseMinOrMax.Min: desiredLocalResistance = measurements.Min(m => m.Resistance) - 1; break; case UseMinOrMax.Max: desiredLocalResistance = measurements.Max(m => m.Resistance) + 1; break; default: throw new ArgumentOutOfRangeException(nameof(minOrMax), minOrMax, null); } var expectedPartialExceptionMessage = $"Cannot calculate mask intensity because given desired resistance value of '{desiredLocalResistance}'" + $" is not within range (Min:'{minResistance}' Max:'{maxResistance}') " + "of loaded measurements."; //When var action = new Action(() => { maskIntensityInterpolator.GetLocalMaskIntensity(desiredLocalResistance, relativePointMinMax); }); //Then AssertExt.ThrowsException <InvalidOperationException>(action, expectedPartialExceptionMessage); }
public void TestInvokeMemberField() { Type type = typeof(TestClass); Type subType = typeof(TestSubClass); TestSubClass targetSub = new TestSubClass(); TestClass target = targetSub; // 测试父类。 // Default Assert.AreEqual(target.TestField, type.InvokeMember("TestField", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); Assert.AreEqual(target.TestField2, type.InvokeMember("TestField2", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); Assert.AreEqual(target.TestField3, type.InvokeMember("TestField3", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); Assert.AreEqual(target.TestField4, type.InvokeMember("TestField4", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); // Explicit Assert.AreEqual(target.TestField, type.InvokeMember("TestField", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); Assert.AreEqual(target.TestField2, type.InvokeMember("TestField2", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); Assert.AreEqual(target.TestField3, type.InvokeMember("TestField3", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); Assert.AreEqual(target.TestField4, type.InvokeMember("TestField4", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); // 测试子类。 // Default Assert.AreEqual(targetSub.TestField, subType.InvokeMember("TestField", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); Assert.AreEqual(targetSub.TestField2, subType.InvokeMember("TestField2", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); Assert.AreEqual(targetSub.TestField3, subType.InvokeMember("TestField3", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); Assert.AreEqual(targetSub.TestField4, subType.InvokeMember("TestField4", BindingFlags.GetField, PowerBinder.Default, target, new object[0])); // Explicit Assert.AreEqual(targetSub.TestField, subType.InvokeMember("TestField", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); Assert.AreEqual(targetSub.TestField2, subType.InvokeMember("TestField2", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); Assert.AreEqual(targetSub.TestField3, subType.InvokeMember("TestField3", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); Assert.AreEqual(targetSub.TestField4, subType.InvokeMember("TestField4", BindingFlags.GetField, PowerBinder.Explicit, target, new object[0])); // 测试设置父类字段。 // Default type.InvokeMember("TestField", BindingFlags.SetField, PowerBinder.Default, target, new object[] { "TestClass0" }); Assert.AreEqual("TestClass0", target.TestField); type.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Default, target, new object[] { (short)20 }); Assert.AreEqual((short)20, target.TestField2); AssertExt.ThrowsException(() => type.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Default, target, new object[] { 20 }), typeof(ArgumentException), typeof(InvalidCastException)); type.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Default, target, new object[] { 200 }); Assert.AreEqual(200L, target.TestField3); type.InvokeMember("TestField4", BindingFlags.SetField, PowerBinder.Default, target, new object[] { "TestClass44" }); Assert.AreEqual("TestClass44", target.TestField4); // Explicit type.InvokeMember("TestField", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { "TestClass0" }); Assert.AreEqual("TestClass0", target.TestField); type.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 20 }); Assert.AreEqual((short)20, target.TestField2); type.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 200 }); Assert.AreEqual(200L, target.TestField3); type.InvokeMember("TestField4", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { "TestClass44" }); Assert.AreEqual("TestClass44", target.TestField4); // 测试设置子类字段。 // Default subType.InvokeMember("TestField", BindingFlags.SetField, PowerBinder.Default, target, new object[] { "TestSubClass0" }); Assert.AreEqual("TestSubClass0", targetSub.TestField); subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Default, target, new object[] { 21 }); Assert.AreEqual(21, targetSub.TestField2); subType.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Default, target, new object[] { 201 }); Assert.AreEqual(201, targetSub.TestField3); subType.InvokeMember("TestField4", BindingFlags.SetField, PowerBinder.Default, target, new object[] { 202 }); Assert.AreEqual(202, targetSub.TestField4); // Explicit subType.InvokeMember("TestField", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { "TestSubClass0" }); Assert.AreEqual("TestSubClass0", targetSub.TestField); subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 21 }); Assert.AreEqual(21, targetSub.TestField2); subType.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 201 }); Assert.AreEqual(201, targetSub.TestField3); subType.InvokeMember("TestField4", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 202 }); Assert.AreEqual(202, targetSub.TestField4); // 测试设置子类和父类字段。 // Default subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Default, target, new object[] { (short)22 }); Assert.AreEqual((short)22, target.TestField2); AssertExt.ThrowsException(() => subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Default, target, new object[] { 23L }), typeof(MissingFieldException), typeof(ArgumentException), typeof(InvalidCastException)); subType.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Default, target, new object[] { 202L }); Assert.AreEqual(202L, target.TestField3); subType.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Default, target, new object[] { (short)203 }); Assert.AreEqual(203, targetSub.TestField3); subType.InvokeMember("TestField4", BindingFlags.SetField, PowerBinder.Default, target, new object[] { "TestClass00" }); Assert.AreEqual("TestClass00", target.TestField4); // Explicit subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { (short)22 }); Assert.AreEqual((short)22, target.TestField2); subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 23L }); Assert.AreEqual(23, targetSub.TestField2); subType.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 202L }); Assert.AreEqual(202L, target.TestField3); subType.InvokeMember("TestField3", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { (short)203 }); Assert.AreEqual(203, targetSub.TestField3); subType.InvokeMember("TestField4", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { "TestClass00" }); Assert.AreEqual("TestClass00", target.TestField4); // 测试强制类型转换。 // Default AssertExt.ThrowsException(() => subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Default, target, new object[] { Tristate.True }), typeof(MissingFieldException), typeof(ArgumentException), typeof(InvalidCastException)); // Explicit subType.InvokeMember("TestField2", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { Tristate.True }); Assert.AreEqual((int)Tristate.True, targetSub.TestField2); AssertExt.ThrowsException(() => subType.InvokeMember("TestField", BindingFlags.SetField, PowerBinder.Explicit, target, new object[] { 22 }), typeof(MissingFieldException)); }
public void TestInvokeMemberMethod() { Type type = typeof(TestSubClass); BindingFlags bindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod; BindingFlags bindingOptFlags = bindingFlags | BindingFlags.OptionalParamBinding; // 测试完整的调用。 Assert.AreEqual(TestSubClass.TestMethod(), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[0])); Assert.AreEqual(TestSubClass.TestMethod(10), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 10 })); Assert.AreEqual(TestSubClass.TestMethod(10), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Explicit, null, new object[] { 10 })); Assert.AreEqual(TestSubClass.TestMethod(10, 20), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 10, 20 })); Assert.AreEqual(TestSubClass.TestMethod(10, 20), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Explicit, null, new object[] { 10, 20 })); Assert.AreEqual(TestSubClass.TestMethod(30, null, true), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 30, null, true })); // 测试完整的调用与类型转换。 Type[] invalidCastExceptions = { typeof(ArgumentException), typeof(InvalidCastException), typeof(MissingMethodException) }; Assert.AreEqual(TestSubClass.TestMethod(10), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { (short)10 })); Assert.AreEqual(TestSubClass.TestMethod(10), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Explicit, null, new object[] { (short)10 })); AssertExt.ThrowsException(() => type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 10L }), invalidCastExceptions); Assert.AreEqual(TestSubClass.TestMethod(10), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Explicit, null, new object[] { 10L })); Assert.AreEqual(TestSubClass.TestMethod(10, 20), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { (short)10, (byte)20 })); AssertExt.ThrowsException(() => type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 10UL, 20L }), invalidCastExceptions); AssertExt.ThrowsException(() => type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 10, 20L }), invalidCastExceptions); Assert.AreEqual(TestSubClass.TestMethod(10, 20), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Explicit, null, new object[] { 10UL, 20L })); // 测试命名参数。 Assert.AreEqual(TestSubClass.TestMethod(30, "str", true), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 30, "str", true })); Assert.AreEqual(TestSubClass.TestMethod(value2: "str", value1: 30, value3: true), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { "str", 30, true }, null, null, new[] { "value2", "value1", "value3" })); Assert.AreEqual(TestSubClass.TestMethod(30, "str", true), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { "str", 30, true }, null, null, new[] { "value2" })); Assert.AreEqual(TestSubClass.TestMethod(value3: true, value2: "str", value1: 30), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { true, "str", 30 }, null, null, new[] { "value3", "value2", "value1" })); Assert.AreEqual(TestSubClass.TestMethod(30, "str", true), type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { true, "str", 30 }, null, null, new[] { "value3", "value2" })); AssertExt.ThrowsException(() => type.InvokeMember("TestMethod", bindingFlags, PowerBinder.Default, null, new object[] { 10 }, null, null, new[] { "values" }), typeof(MissingMethodException)); // 测试默认参数和 params 参数。 Assert.AreEqual(TestSubClass.TestMethod2(30, "str", true, 1, 2, 3), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", true, 1, 2, 3 })); Assert.AreEqual(TestSubClass.TestMethod2(30, "str", true), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", true })); Assert.AreEqual(TestSubClass.TestMethod2(30, "str"), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str" })); Assert.AreEqual(TestSubClass.TestMethod2(30), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30 })); AssertExt.ThrowsException(() => type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[0]), typeof(MissingMethodException)); // 测试命名参数、默认参数和 params 参数。 Assert.AreEqual(TestSubClass.TestMethod2(value1: 30, value2: "str", value3: true, value4: new int[] { 1, 2, 3 }), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", true, 1, 2, 3 }, null, null, new[] { "value1", "value2", "value3", "value4" })); Assert.AreEqual(TestSubClass.TestMethod2(30, "str", true, 1, 2, 3), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", true, 1, 2, 3 }, null, null, new[] { "value1", "value2", "value3" })); Assert.AreEqual(TestSubClass.TestMethod2(value1: 30, value2: "str", value3: true), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", true }, null, null, new[] { "value1", "value2", "value3" })); Assert.AreEqual(TestSubClass.TestMethod2(30, "str", true, 1, 2, 3), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", 1, true, 2, 3 }, null, null, new[] { "value1", "value2", "value4" })); Assert.AreEqual(TestSubClass.TestMethod2(value1: 30, value2: "str", value4: new[] { 1 }), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", new int[] { 1 } }, null, null, new[] { "value1", "value2", "value4" })); Assert.AreEqual(TestSubClass.TestMethod2(value4: new[] { 1 }, value3: true, value1: 30), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { new int[] { 1 }, true, 30 }, null, null, new[] { "value4", "value3", "value1" })); Assert.AreEqual(TestSubClass.TestMethod2(value4: new[] { 1 }, value1: 30), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { new int[] { 1 }, 30 }, null, null, new[] { "value4", "value1" })); Assert.AreEqual(TestSubClass.TestMethod2(value3: true, value1: 30), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { true, 30 }, null, null, new string[] { "value3", "value1" })); Assert.AreEqual(TestSubClass.TestMethod2(value1: 30), type.InvokeMember("TestMethod2", bindingOptFlags, PowerBinder.Default, null, new object[] { 30 }, null, null, new[] { "value1" })); // 测试泛型方法。 Assert.AreEqual(TestSubClass.TestMethod3(10, 20), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { 10, 20 })); Assert.AreEqual(TestSubClass.TestMethod3(10, 20), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { (short)10, 20 })); Assert.AreEqual(TestSubClass.TestMethod3((short)10, (short)20), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { (short)10, (short)20 })); Assert.AreEqual(TestSubClass.TestMethod3(10, (long)20), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { (short)10, (long)20 })); Assert.AreEqual(TestSubClass.TestMethod3(10, "str"), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { 10, "str" })); Assert.AreEqual(TestSubClass.TestMethod3(10, "str"), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { (short)10, "str" })); AssertExt.ThrowsException(() => type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { (long)10, "str" }), typeof(MissingMethodException)); Assert.AreEqual(TestSubClass.TestMethod3("text", "str"), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { "text", "str" })); AssertExt.ThrowsException(() => type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { "str", 10 }), typeof(MissingMethodException)); Assert.AreEqual(TestSubClass.TestMethod3("text", "str", 10), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { "text", "str", 10 })); Assert.AreEqual(TestSubClass.TestMethod3(10, 20, 30), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { 10, 20, 30 })); Assert.AreEqual(TestSubClass.TestMethod3(10, 20, "str"), type.InvokeMember("TestMethod3", bindingFlags, PowerBinder.Default, null, new object[] { 10, 20, "str" })); Assert.AreEqual("<System.Int32, System.String, System.Int32>(10, str, 20,30)", type.InvokeMember("TestMethod5", bindingOptFlags, PowerBinder.Default, null, new object[] { 10, "str", 20, 30 })); Assert.AreEqual("<System.Int32, System.String, System.Int64>(10, str, 20,30)", type.InvokeMember("TestMethod5", bindingOptFlags, PowerBinder.Default, null, new object[] { 10, "str", 20L, 30 })); Assert.AreEqual("<System.Int32, System.String, System.Int32>(10, test, 20,30)", type.InvokeMember("TestMethod5", bindingOptFlags, PowerBinder.Default, null, new object[] { 10, new int[] { 20, 30 }, "test" }, null, null, new string[] { "value1", "value3", "value2" })); // 测试选择方法。 BindingFlags bindingInsFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod; BindingFlags bindingInsOptFlags = bindingInsFlags | BindingFlags.OptionalParamBinding; TestSubClass subClass = new TestSubClass(); Assert.AreEqual(subClass.TestMethod4(10, 20), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Explicit, subClass, new object[] { 10, 20 })); Assert.AreEqual(subClass.TestMethod4(true, "str"), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Explicit, subClass, new object[] { true, "str" })); Assert.AreEqual(subClass.TestMethod4(10, "str"), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Explicit, subClass, new object[] { 10, "str" })); Assert.AreEqual(subClass.TestMethod4(10, "str"), type.InvokeMember("TestMethod4", bindingInsOptFlags, PowerBinder.Explicit, subClass, new object[] { 10, "str" })); Assert.AreEqual(subClass.TestMethod4(10, "str", false), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Explicit, subClass, new object[] { 10, "str", false })); Assert.AreEqual(subClass.TestMethod4(10, "str", 1), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Explicit, subClass, new object[] { 10, "str", 1 })); Assert.AreEqual(subClass.TestMethod4(10, "str", new int[] { 1 }), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Explicit, subClass, new object[] { 10, "str", new int[] { 1 } })); Assert.AreEqual(subClass.TestMethod4(10, "str", 1, 2), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Explicit, subClass, new object[] { 10, "str", 1, 2 })); Assert.AreEqual(subClass.TestMethod4(true, true), type.InvokeMember("TestMethod4", bindingInsFlags, PowerBinder.Default, subClass, new object[] { true, true })); Assert.AreEqual(TestSubClass.TestMethod6(value2: 30, value1: "str"), type.InvokeMember("TestMethod6", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str" }, null, null, new string[] { "value2", "value1" })); Assert.AreEqual(TestSubClass.TestMethod6(value2: (short)30, value1: "str"), type.InvokeMember("TestMethod6", bindingOptFlags, PowerBinder.Default, null, new object[] { (short)30, "str" }, null, null, new string[] { "value2", "value1" })); Assert.AreEqual(TestSubClass.TestMethod6(value2: 30, value1: "str", value3: "str2"), type.InvokeMember("TestMethod6", bindingOptFlags, PowerBinder.Default, null, new object[] { 30, "str", "str2" }, null, null, new string[] { "value2", "value1" })); }