public void Can_Get_Field_Value() { var test = new ReflectionTestClass(); var value = ReflectionHelper.GetField <string>(test, "TestField"); Assert.AreEqual("TestValue", value); }
public void GetMethod_InstanceFunction_ReturnsLambdaForRequestedFunction() { int a = Random.Next(0, 10); int b = Random.Next(0, 10); ReflectionTestClass <String> testInstance = new ReflectionTestClass <String>("Test"); // Set the expected output for the dummy testInstance.InstanceFunctionOutput = a + b; Func <ReflectionTestClass <String>, int, int, int> func = typeof(ReflectionTestClass <String>).GetMethod("InstanceFunction").Func <ReflectionTestClass <String>, int, int, int>(); Assert.IsNotNull(func, "The lambda returned must not be null."); // Note that there is no simple way to test if we have the correct function, so we instead test that parameters are received correctly Assert.AreEqual( a + b, func(testInstance, a, b), "When called, the lambda returned by GetMethod should return the value the requested function returns."); Assert.AreEqual( a, testInstance.InstanceFunctionInputA, "When called with parameters, the lambda calls the requested function using these parameters."); Assert.AreEqual( b, testInstance.InstanceFunctionInputB, "When called with parameters, the lambda calls the requested function using these parameters."); }
public void Can_Get_Property_Value() { var test = new ReflectionTestClass(); var value = ReflectionHelper.GetProperty <string>(test, "TestProperty"); Assert.AreEqual("TestValue", value); }
public void Can_Get_Field_Value() { var test = new ReflectionTestClass(); var value = ReflectionHelper.GetField<string>(test, "TestField"); Assert.AreEqual("TestValue", value); }
public void Can_Invoke_Method() { var test = new ReflectionTestClass(); ReflectionHelper.InvokeMethod(test, "Method"); Assert.That(test.MethodInvoked); }
public void GetMethod_Constructor_ReturnsLambdaForRequestedConstructor() { Func <Guid, ReflectionTestClass <Guid> > constructor = typeof(ReflectionTestClass <Guid>).ConstructorFunc <Guid, ReflectionTestClass <Guid> >(); Guid guid = Guid.NewGuid(); ReflectionTestClass <Guid> testInstance = constructor(guid); Assert.AreEqual(guid, testInstance.ID); }
public void GetSetter_PropertyWithNoSetter_ReturnsNull() { ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Action <ReflectionTestClass <Guid> > setLast = typeof(ReflectionTestClass <Guid>).GetSetter <ReflectionTestClass <Guid> >("Last"); Assert.IsNull(setLast); }
public virtual void TestReflection() { IUnityContainer container = this.GetContainer(); ReflectionTestClass testClass = container.Resolve <ReflectionTestClass>(); Type interceptedType = testClass.GetType(); Assert.AreNotEqual(typeof(ReflectionTestClass), interceptedType); Assert.IsTrue(interceptedType.IsSubclassOf(typeof(ReflectionTestClass))); }
public void GetSetter_PropertyWithPrivateGetter_ReturnsLambdaWhichReturnsNewValueOfSpecifiedProperty() { int value = Random.Next(); ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Action <ReflectionTestClass <Guid>, int> setB = typeof(ReflectionTestClass <Guid>).GetSetter <ReflectionTestClass <Guid>, int>("B"); setB(testInstance, value); }
public void GetSetter_PropertyWithPrivateSetter_ReturnsLambdaForRequestedProperty() { DateTime value = DateTime.Now; ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Action <DateTime> setDateTime = typeof(ReflectionTestClass <Guid>).GetSetter <DateTime>("DateTime"); setDateTime(value); Assert.AreEqual(value, ReflectionTestClass <Guid> .DateTime); }
public void GetSetter_PropertyWithExplicitSetter_ReturnsLambdaForRequestedProperty() { int value = Random.Next(); ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Action <ReflectionTestClass <Guid>, int> setC = typeof(ReflectionTestClass <Guid>).GetSetter <ReflectionTestClass <Guid>, int>("C"); setC(testInstance, value); Assert.AreEqual(testInstance.SetC, value); }
public void GetGetter_PropertyWithExplicitGetter_ReturnsLambdaGetterForRequestedProperty() { ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Func <ReflectionTestClass <Guid> > getLast = typeof(ReflectionTestClass <Guid>).GetGetter <ReflectionTestClass <Guid> >("Last"); Assert.AreEqual( testInstance, getLast(), "The lambda function returned by GetGetter should return the value of the requested property."); }
public void GetGetter_Field_ReturnsLambdaGetterForRequestedField() { int value = Random.Next(); ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Func <ReflectionTestClass <Guid>, int> getA = typeof(ReflectionTestClass <Guid>).GetGetter <ReflectionTestClass <Guid>, int>("A"); testInstance.A = value; Assert.AreEqual( testInstance.A, getA(testInstance), "The lambda function returned by GetGetter should return the value of the field given as the parameter."); }
public void GetGetter_PropertyWithPrivateGetter_ReturnsLambdaGetterForRequestedProperty() { int value = Random.Next(); ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Func <ReflectionTestClass <Guid>, int> getB = typeof(ReflectionTestClass <Guid>).GetGetter <ReflectionTestClass <Guid>, int>("B"); testInstance.B = value; Assert.AreEqual( value, getB(testInstance), "The lambda function returned by GetGetter should return the value of the requested property."); }
public void GetSetter_Field_ReturnsLambdaForRequestedField() { int value = Random.Next(); ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Action <ReflectionTestClass <Guid>, int> setA = typeof(ReflectionTestClass <Guid>).GetSetter <ReflectionTestClass <Guid>, int>("A"); setA(testInstance, value); Assert.AreEqual( value, testInstance.A, "The lambda function returned by GetSetter should change the value of the specified field."); }
public void GetMethod_InstanceMethod_ReturnsLambdaForRequestedMethod() { string value = Random.Next().ToString(); ReflectionTestClass <Guid> testInstance = new ReflectionTestClass <Guid>(Guid.NewGuid()); Action <ReflectionTestClass <Guid>, string> method = typeof(ReflectionTestClass <Guid>).GetMethod("InstanceMethod").Action <ReflectionTestClass <Guid>, string> (); Assert.IsNotNull(method, "The lambda returned must not be null."); // Note that there is no simple way to test if we have the correct function, so we instead test that parameters are received correctly method(testInstance, value); Assert.AreEqual( value, testInstance.InstanceMethodInput, "When called with parameters, the lambda calls the requested method using these parameters."); }
public void Can_Get_Property_Value() { var test = new ReflectionTestClass(); var value = ReflectionHelper.GetProperty<string>(test, "TestProperty"); Assert.AreEqual("TestValue", value); }
public ReflectionTestClass() { _last = this; }
public ReflectionTestClass(T id) { ID = id; _last = this; }