Ejemplo n.º 1
0
        /// <summary>
        /// Format filter.
        /// </summary>
        /// <param name="obj">Object.</param>
        /// <param name="filterOperator">Filter operator.</param>
        /// <returns></returns>
        internal string FormatFilter(object obj, FilterOperator filterOperator)
        {
            IFilterFormatter formatter = PropertyDefinition.formatterProvider[this.Type.FullName];

            return(formatter.Format(
                       obj,
                       filterOperator,
                       this));
        }
        private void TestFilterFormatter(
            Type type,
            Type typeItHandle,
            PropertyDefinition propertyDefinition,
            object objectToFormat,
            FilterOperator filterOperator,
            string expectedResult)
        {
            ArgumentValidator.ThrowIfNull(
                type,
                nameof(type));

            if (!type.IsSubclassOf(typeof(BaseFilterFormatter)))
            {
                throw new ArgumentException("Please provide typeof(BaseFilterFormatter)");
            }

            if (type.IsAbstract)
            {
                throw new ArgumentException("Please provide non-abstract filter formatter type.");
            }

            IFilterFormatter formatter = (IFilterFormatter)Activator.CreateInstance(type);

            Assert.AreEqual(
                typeItHandle.FullName,
                formatter.Type);

            string formattedString = formatter.Format(
                objectToFormat,
                filterOperator,
                propertyDefinition);

            Assert.AreEqual(
                expectedResult,
                formattedString);

            Assert.ThrowsException <ArgumentNullException>(() =>
            {
                formatter.Format(
                    null,
                    filterOperator,
                    propertyDefinition);
            });

            Assert.ThrowsException <ArgumentException>(() =>
            {
                formatter.Format(
                    new TempClass(),
                    filterOperator,
                    propertyDefinition);
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create new instance of <see cref="FormatProvider"/>
        /// </summary>
        internal FormatterProvider()
        {
            this.formatters = new Dictionary <string, IFilterFormatter>();

            Type baseFilterFormatterType = typeof(BaseFilterFormatter);

            foreach (Type type in Assembly.GetAssembly(baseFilterFormatterType).GetTypes())
            {
                if (type.IsClass &&
                    !type.IsAbstract &&
                    type.IsSubclassOf(baseFilterFormatterType))
                {
                    IFilterFormatter formatter = (BaseFilterFormatter)Activator.CreateInstance(type);
                    this.formatters.Add(formatter.Type, formatter);
                }
            }
        }