public void AddPropertyToDerivedClass() { // Arrange _typeExtender = new TypeExtender("ClassA"); _typeExtender.AddProperty("IsAdded", typeof(bool)); _typeExtender.AddProperty("IsEnabled", typeof(bool), true); _typeExtender.AddProperty <double>("Length"); _typeExtender.AddProperty <double>("Width", true); //Act var returnedClass = _typeExtender.FetchType(); var isAddedProperty = returnedClass.GetProperty("IsAdded"); var isEnabledProperty = returnedClass.GetProperty("IsEnabled"); var lengthProperty = returnedClass.GetProperty("Length"); var widthProperty = returnedClass.GetProperty("Width"); //Assert Assert.AreEqual("IsAdded", isAddedProperty.Name); Assert.AreEqual(typeof(bool), isAddedProperty.PropertyType); Assert.AreEqual("IsEnabled", isEnabledProperty.Name); Assert.AreEqual(typeof(bool), isEnabledProperty.PropertyType); Assert.AreEqual(false, isEnabledProperty.CanWrite); Assert.AreEqual("Length", lengthProperty.Name); Assert.AreEqual(typeof(double), lengthProperty.PropertyType); Assert.AreEqual("Width", widthProperty.Name); Assert.AreEqual(typeof(double), widthProperty.PropertyType); Assert.AreEqual(false, widthProperty.CanWrite); }
public void AddPropertyByGenericMethodWithMultipleAttributes() { var attributeTypeA = typeof(CustomAAttribute); var attributeParamsA = new object[] { "Jon Snow" }; var attributeTypeB = typeof(CustomBAttribute); _typeExtender = new TypeExtender("ClassA"); var attributesWithValues = new[] { new Tuple <Type, object[]>(attributeTypeA, attributeParamsA), new Tuple <Type, object[]>(attributeTypeB, new object[] { }) }; _typeExtender.AddProperty <bool>("IsAdded", attributesWithValues); var returnedClass = _typeExtender.FetchType(); var property = returnedClass.GetProperty("IsAdded"); var attributesOfTypA = property.GetCustomAttributes(attributeTypeA, false); var attributeA = attributesOfTypA[0] as CustomAAttribute; Assert.AreEqual(1, attributesOfTypA.Length); Assert.NotNull(attributeA); Assert.AreEqual("Jon Snow", attributeA.Name); var attributesOfTypB = property.GetCustomAttributes(attributeTypeB, false); var attributeB = attributesOfTypB[0] as CustomBAttribute; Assert.AreEqual(1, attributesOfTypB.Length); Assert.NotNull(attributeB); }
public void AddACollectionOfPropertiesWithSameType() { _typeExtender = new TypeExtender("ClassA"); var properites1 = new string[] { "IsEnabled", "CanFollowUp", "IsAdded" }; var properties2 = new string[] { "Length", "Width", "Height" }; _typeExtender.AddProperty(properites1, typeof(bool)); _typeExtender.AddProperty <double>(properties2); var returnedClass = _typeExtender.FetchType(); var properties = returnedClass.GetProperties(); var all = properites1.Union(properties2); foreach (var prop in all) { Assert.Contains(prop, properties.Select(x => x.Name).ToList()); } }
public void AddPropertyWithAttribute() { var attributeType = typeof(CustomAAttribute); var attributeParams = new object[] { "Jon Snow" }; _typeExtender = new TypeExtender("ClassA"); _typeExtender.AddProperty("IsAdded", typeof(bool), attributeType, attributeParams); var returnedClass = _typeExtender.FetchType(); var property = returnedClass.GetProperty("IsAdded"); var attributes = property.GetCustomAttributes(attributeType, false); var attribute = attributes[0] as CustomAAttribute; Assert.AreEqual(1, attributes.Length); Assert.NotNull(attribute); Assert.AreEqual("Jon Snow", attribute.Name); }
public void AddType() { var className = "MES.Client.Core.Model.ClientEntityBase"; _typeExtender = new TypeExtender(className); _typeExtender.AddProperty("IsAdded", typeof(bool)); //_typeExtender.AddProperty("IsEnabled", typeof(bool), true); //_typeExtender.AddProperty<double>("Length"); //_typeExtender.AddProperty<double>("Width", true); //Act var returnedClass = _typeExtender.FetchType(); var isAddedProperty = returnedClass.GetProperty("IsAdded"); var isEnabledProperty = returnedClass.GetProperty("IsEnabled"); var lengthProperty = returnedClass.GetProperty("Length"); var widthProperty = returnedClass.GetProperty("Width"); baseTextBox.Text = isAddedProperty.ToString(); }