protected override void VerifyDeserialized <TValueType>(TValueType deserializedValue, TValueType expectedValue, MessageSerializedPropertyInfo propertyInfo, byte[] valueArray) { // The "Kind" property after deserialization is "Unspecified" so we need to ignore that part TestSubClass deserializedSubClass = deserializedValue as TestSubClass; TestSubClass expectedSubClass = expectedValue as TestSubClass; Assert.That(deserializedSubClass, Is.Not.Null, "DeserializedTestSubClass"); Assert.That(expectedSubClass, Is.Not.Null, "ExpectedTestSubClass"); Assert.That(expectedSubClass.Length, Is.Not.EqualTo(0), "Length 0"); Assert.That(deserializedSubClass.Length, Is.EqualTo(expectedSubClass.Length), "Length"); Assert.That(deserializedSubClass.Meters.Count, Is.EqualTo(expectedSubClass.Meters.Count), "MetersCount"); for (int index = 0; index < deserializedSubClass.Meters.Count; ++index) { TestSubClass.MetersBlob deserializedMeterBlob = deserializedSubClass.Meters[index]; TestSubClass.MetersBlob expectedMeterBlob = expectedSubClass.Meters[index]; Assert.That(deserializedMeterBlob.MeterLength, Is.EqualTo(expectedMeterBlob.MeterLength), "MeterLength"); Assert.That(deserializedMeterBlob.MeterNumber, Is.EqualTo(expectedMeterBlob.MeterNumber), "MeterNumber"); Assert.That(deserializedMeterBlob.BcdMeterValue, Is.EqualTo(expectedMeterBlob.BcdMeterValue), "BcdMeterValue"); } Assert.That(deserializedSubClass.SomeSubClass.Field1, Is.EqualTo(expectedSubClass.SomeSubClass.Field1), "Field1"); Assert.That(deserializedSubClass.SomeSubClass.Field2, Is.EqualTo(expectedSubClass.SomeSubClass.Field2), "Field2"); }
public void How_to_get_the_declared_class_name_when_calling_a_Static_method() { var className = TestClass.GetClassNameWhenCallingStaticMethod(); var subClassName = TestSubClass.GetClassNameWhenCallingStaticMethod(); className.ShouldBe(typeof(TestClass).Name); subClassName.ShouldBe(typeof(TestSubClass).Name); }
public void Test_GetValue_VirtualProperty_CalledOnBaseClass() { // Arrange TestBaseClass instance = new TestSubClass(); string testValue = "some test value"; instance.VirtualTestProperty = testValue; Munger munger = new Munger("VirtualTestProperty"); // Act string value = munger.GetValue(instance) as string; // Assert Assert.AreEqual(testValue, value); }
public void Test_PutValue_DottedAspect_Success() { // Arrange TestSubClass instance = new TestSubClass(); string testValue = "some test value"; Munger munger = new Munger("This.This.VirtualTestProperty"); // Act munger.PutValue(instance, testValue); // Assert Assert.AreEqual(testValue, instance.VirtualTestProperty); }
public void Test_PutValue_NonVirtualProperty_CalledOnSubClass() { // Arrange TestSubClass instance = new TestSubClass(); string testValue = "some test value"; Munger munger = new Munger("NonVirtualTestProperty"); // Act munger.PutValue(instance, testValue); // Assert Assert.AreEqual(testValue, instance.NonVirtualTestProperty); Assert.IsTrue(instance.subClassPropertyModified); }
public void Test_GetValue_NonVirtualProperty_CalledOnSubClass() { // Arrange TestSubClass instance = new TestSubClass(); string testValue = "some test value"; instance.NonVirtualTestProperty = testValue; Munger munger = new Munger("NonVirtualTestProperty"); // Act string value = munger.GetValue(instance) as string; // Assert Assert.AreEqual(testValue, value); }
public void Test_PutValue_DottedAspect_CantModifyReadOnlyProperty() { // Arrange TestSubClass instance = new TestSubClass(); string testValue = "some test value"; string testValue2 = "some test value2"; Munger munger = new Munger("This.This.ReadOnlyProperty"); instance.SetReadOnlyProperty(testValue); Assert.AreEqual(testValue, instance.ReadOnlyProperty); // Act munger.PutValue(instance, testValue2); // Assert Assert.AreEqual(testValue, instance.ReadOnlyProperty); }
public void CreateAttachmentInSubCouchDocumentClass() { // Arrange TestSubClass tsc = new TestSubClass { TESTVAL = "Hello" }; tsc = _db.CreateDocument(tsc, new Result<TestSubClass>()).Wait(); // Act var attachment = Encoding.UTF8.GetBytes("test"); using (MemoryStream ms = new MemoryStream( Encoding.UTF8.GetBytes("This is a text document"))) { _db.AddAttachment(tsc.Id, ms, "test_upload.txt"); } tsc = _db.GetDocument(tsc.Id, new Result<TestSubClass>()).Wait(); // Assert Assert.IsTrue(tsc.HasAttachment); }
public void ForEach_applies_only_to_specified_subtype() { var t1 = new TestSubClass(); var t2 = new TestSubClass(); _list.Add(new TestBaseClass()); _list.Add(new TestBaseClass()); _list.Add(t1); _list.Add(new TestBaseClass()); _list.Add(new OtherSubClass()); _list.Add(new TestBaseClass()); _list.Add(t2); _list.ForEach(_action); _action.Received(1).Invoke(t1); _action.Received(1).Invoke(t2); _action.DidNotReceive().Invoke(Arg.Is<TestSubClass>(x => !x.Equals(t1) && !x.Equals(t2))); }
static void Main() { TestClass obj = new TestClass(); TestSubClass subObj = new TestSubClass(); OtherSubClass otherObj = new OtherSubClass(); obj.TestMethod(); subObj.TestMethod(); otherObj.TestMethod(); try { RedirectionHelper.RedirectCalls(typeof(TestSubClass).GetMethod("TestMethod"), typeof(DetourClass).GetMethod("TestMethod")); obj.TestMethod(); subObj.TestMethod(); otherObj.TestMethod(); } catch (Exception e) { Console.Out.WriteLine(e); } }
static void Main() { TestClass obj = new TestClass(); TestSubClass subObj = new TestSubClass(); OtherSubClass otherObj = new OtherSubClass(); obj.TestMethod(); subObj.TestMethod(); otherObj.TestMethod(); try { RedirectionHelper.RedirectCalls(typeof(TestSubClass).GetMethod("TestMethod"), typeof(DetourClass).GetMethod("TestMethod")); obj.TestMethod(); subObj.TestMethod(); otherObj.TestMethod(); } catch (Exception e) { Console.Out.WriteLine(e); } }
public void GetByIdTest() { // When Id is set the object is returned TestSubClass testSubClass = new TestSubClass { id = "theId", name = "theName" }; TestClass theObject = new TestClass { PropertyInt = 7, PropertyString = "Test", Sub = testSubClass }; TestSubClass actual = ObjectLookupHelper.GetById <TestSubClass>(theObject, testSubClass.id); Assert.AreEqual(theObject.Sub, actual); // Id is null, returns null testSubClass = new TestSubClass { id = null, name = "theName" }; theObject = new TestClass { PropertyInt = 7, PropertyString = "Test", Sub = testSubClass }; actual = ObjectLookupHelper.GetById <TestSubClass>(theObject, testSubClass.id); Assert.AreEqual(null, actual); // Id is set in Subs, that Sub is returned testSubClass = new TestSubClass { id = "theId", name = "theName" }; theObject = new TestClass { PropertyInt = 7, PropertyString = "Test", Sub = null, Subs = new TestSubClass[] { testSubClass } }; actual = ObjectLookupHelper.GetById <TestSubClass>(theObject, testSubClass.id); Assert.AreEqual(theObject.Subs[0], actual); // This time the Id is not found testSubClass = new TestSubClass { id = "theId", name = "theName" }; theObject = new TestClass { PropertyInt = 7, PropertyString = "Test", Sub = null, Subs = new TestSubClass[] { testSubClass } }; actual = ObjectLookupHelper.GetById <TestSubClass>(theObject, "invalid Id"); Assert.AreEqual(null, actual); }
public void Test_PutValue_NonVirtualProperty_CalledOnBaseClass() { // Arrange TestBaseClass instance = new TestSubClass(); string testValue = "some test value"; Munger munger = new Munger("NonVirtualTestProperty"); // Act munger.PutValue(instance, testValue); // Munger acts on the runtime type of the instance, so it (correctly) // calls the subclass' version of this property. The compiler // uses the declared type of the instance, so instance.NonVirtualTestProperty // returns null, which is correct but perhaps unexpected. // Assert Assert.AreEqual(null, instance.NonVirtualTestProperty); Assert.AreEqual(testValue, (instance as TestSubClass).NonVirtualTestProperty); Assert.IsTrue((instance as TestSubClass).subClassPropertyModified); }
public void ShouldCreateAttachmentInSubCouchDocumentClass() { var db = client.GetDatabase(baseDatabase); TestSubClass tsc = new TestSubClass { TESTVAL = "Hello" }; tsc = db.CreateDocument(tsc, new Result <TestSubClass>()).Wait(); var attachment = Encoding.UTF8.GetBytes("test"); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("This is a text document"))) { db.AddAttachment(tsc.Id, ms, "test_upload.txt"); } tsc = db.GetDocument(tsc.Id, new Result <TestSubClass>()).Wait(); Assert.IsTrue(tsc.HasAttachment); }
public void Test_PutValue_NonVirtualProperty_CalledOnBaseClass() { // Arrange TestBaseClass instance = new TestSubClass(); string testValue = "some test value"; Munger munger = new Munger("NonVirtualTestProperty"); // Act munger.PutValue(instance, testValue); // Munger acts on the runtime type of the instance, so it (correctly) // calls the subclass' version of this property. The compiler // uses the declared type of the instance, so instance.NonVirtualTestProperty // returns null, which is correct but perhaps unexpected. // Assert Assert.AreEqual(null, instance.NonVirtualTestProperty); Assert.AreEqual(testValue, (instance as TestSubClass).NonVirtualTestProperty); Assert.IsTrue((instance as TestSubClass).subClassPropertyModified); }
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" })); }
public void ReadSubClass() { TestClass testClass = (TestClass)NetReflector.Read(TestClass.GetXmlWithSubClass(DateTime.Now), table); TestSubClass.AssertEquals(TestSubClass.Create(), (TestSubClass)testClass.InnerClass); }
public void ShouldCreateAttachmentInSubCouchDocumentClass() { var db = client.GetDatabase(baseDatabase); TestSubClass tsc = new TestSubClass {TESTVAL = "Hello"}; tsc = db.CreateDocument(tsc, new Result<TestSubClass>()).Wait(); var attachment = Encoding.UTF8.GetBytes("test"); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("This is a text document"))) { db.AddAttachment(tsc.Id, ms, "test_upload.txt"); } tsc = db.GetDocument(tsc.Id, new Result<TestSubClass>()).Wait(); Assert.IsTrue(tsc.HasAttachment); }
protected void TestField(MessageSerializedClassInfo classInfo, string propertyName, TestSubClass valueToUse, byte[] expectedArray) { MessageSerializedPropertyInfo propertyInfo = GetPropertyInfo(classInfo, propertyName); TypeSerializerSerializableClass <TestSubClass> typeSerializer = new TypeSerializerSerializableClass <TestSubClass>(propertyInfo); TestField(typeSerializer, propertyInfo, valueToUse, expectedArray); }
public void Test_PutValue_DottedAspect_Success() { // Arrange TestSubClass instance = new TestSubClass(); string testValue = "some test value"; Munger munger = new Munger("This.This.VirtualTestProperty"); // Act munger.PutValue(instance, testValue); // Assert Assert.AreEqual(testValue, instance.VirtualTestProperty); }
public void Test_PutValue_DottedAspect_CantModifyReadOnlyProperty() { // Arrange TestSubClass instance = new TestSubClass(); string testValue = "some test value"; string testValue2 = "some test value2"; Munger munger = new Munger("This.This.ReadOnlyProperty"); instance.SetReadOnlyProperty(testValue); Assert.AreEqual(testValue, instance.ReadOnlyProperty); // Act munger.PutValue(instance, testValue2); // Assert Assert.AreEqual(testValue, instance.ReadOnlyProperty); }
public void Test_PutValue_NonVirtualProperty_CalledOnSubClass() { // Arrange TestSubClass instance = new TestSubClass(); string testValue = "some test value"; Munger munger = new Munger("NonVirtualTestProperty"); // Act munger.PutValue(instance, testValue); // Assert Assert.AreEqual(testValue, instance.NonVirtualTestProperty); Assert.IsTrue(instance.subClassPropertyModified); }