Example #1
0
        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);
        }
Example #2
0
        public void ReturnATypeWithThePassedName()
        {
            var className = "ClassA";

            _typeExtender = new TypeExtender(className);

            var returnedClass = _typeExtender.FetchType();
            var name          = returnedClass.Name;

            Assert.AreEqual(className, name);
        }
Example #3
0
        public void AddAttributesWithParamsToDerivedClass()
        {
            _typeExtender = new TypeExtender("ClassA");
            _typeExtender.AddAttribute <CustomAAttribute>(new object[] { "Jon Snow" });

            var returnedClass = _typeExtender.FetchType();
            var attributes    = returnedClass.GetCustomAttributes(typeof(CustomAAttribute), false);
            var attribute     = attributes.Single().GetType();

            Assert.AreEqual(typeof(CustomAAttribute).Name, attribute.Name);
            Assert.AreEqual(typeof(CustomAAttribute).FullName, attribute.FullName);
        }
Example #4
0
        public void ReturnATypeWithThePassedNameAndBaseClass()
        {
            var className = "ClassA";
            var baseType  = typeof(List <string>);

            _typeExtender = new TypeExtender(className, baseType);

            var returnedClass    = _typeExtender.FetchType();
            var name             = returnedClass.Name;
            var basetypeReturned = returnedClass.BaseType;

            Assert.AreEqual(className, name);
            Assert.AreEqual(baseType, basetypeReturned);
        }
Example #5
0
        public void AddPropertyWithAddributes()
        {
            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);
        }
Example #6
0
        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());
            }
        }