Esempio n. 1
0
        private void Assertions(FluentObject fluentObject)
        {
            Assert.IsTrue(fluentObject.Name.Equals(NamedFluentModel.MODEL_NAME));
            Assert.IsTrue(fluentObject.ContainsKey(NamedFluentModel.BOOLEAN_PROPERTY_NAME));
            Assert.IsTrue(fluentObject.ContainsKey(NamedFluentModel.DATE_PROPERTY_NAME));
            Assert.IsTrue(fluentObject.ContainsKey(NamedFluentModel.DOUBLE_PROPERTY_NAME));
            Assert.IsTrue(fluentObject.ContainsKey(NamedFluentModel.INTEGER_PROPERTY_NAME));
            Assert.IsTrue(fluentObject.ContainsKey(NamedFluentModel.OBJECT_PROPERTY_NAME));

            Assert.AreEqual(fluentObject[NamedFluentModel.BOOLEAN_PROPERTY_NAME], NamedFluentModel.BOOLEAN_VALUE.ToString());
            Assert.AreEqual(fluentObject[NamedFluentModel.DATE_PROPERTY_NAME], NamedFluentModel.DATE_VALUE.ToString());
            Assert.AreEqual(fluentObject[NamedFluentModel.DOUBLE_PROPERTY_NAME], NamedFluentModel.DOUBLE_VALUE.ToString());
            Assert.AreEqual(fluentObject[NamedFluentModel.INTEGER_PROPERTY_NAME], NamedFluentModel.INTEGER_VALUE.ToString());
            Assert.AreEqual(fluentObject[NamedFluentModel.OBJECT_PROPERTY_NAME], NamedFluentModel.OBJECT_VALUE.ToString());

            var booleanString = "False";
            var dateString    = (new DateTime(2018, 10, 10)).ToString();
            var doubleString  = 100.6.ToString();
            var integerString = 100.ToString();
            var objectString  = new Object().ToString();

            fluentObject[NamedFluentModel.BOOLEAN_PROPERTY_NAME] = booleanString;
            fluentObject[NamedFluentModel.DATE_PROPERTY_NAME]    = dateString;
            fluentObject[NamedFluentModel.DOUBLE_PROPERTY_NAME]  = doubleString;
            fluentObject[NamedFluentModel.INTEGER_PROPERTY_NAME] = integerString;
            fluentObject[NamedFluentModel.OBJECT_PROPERTY_NAME]  = objectString;

            Assert.AreEqual(fluentObject[NamedFluentModel.BOOLEAN_PROPERTY_NAME], booleanString);
            Assert.AreEqual(fluentObject[NamedFluentModel.DATE_PROPERTY_NAME], dateString);
            Assert.AreEqual(fluentObject[NamedFluentModel.DOUBLE_PROPERTY_NAME], doubleString);
            Assert.AreEqual(fluentObject[NamedFluentModel.INTEGER_PROPERTY_NAME], integerString);
            Assert.AreEqual(fluentObject[NamedFluentModel.OBJECT_PROPERTY_NAME], objectString);
        }
Esempio n. 2
0
        /// <summary>
        /// Sets the value of given property for given object
        /// </summary>
        /// <param name="prop">Source property</param>
        /// <param name="source">Source FluentObject</param>
        /// <param name="target">Target object</param>
        public static void SetValue(this PropertyInfo prop, FluentObject source, ref object target)
        {
            if (prop == null)
            {
                throw new ArgumentNullException("prop");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var fluentPropertyAttribute = prop.GetCustomAttributes(typeof(FluentPropertyAttribute), false).SingleOrDefault() as FluentPropertyAttribute;

            if (fluentPropertyAttribute == null)
            {
                throw new ArgumentException("Property is not FluentProperty", "prop");
            }

            object[] converterParameters;
            var      converter = prop.GetConverter(out converterParameters);

            var key = fluentPropertyAttribute.Name ?? prop.Name;

            if (source.ContainsKey(key))
            {
                var valueString = source[key];
                var value       = converter != null?converter.Convert(valueString, converterParameters) : prop.PropertyType.DefaultValue();

                prop.SetValue(target, value);
            }
        }