Пример #1
0
        public void ReferenceTypeCreateAllGetters()
        {
            PropertyGetter <TestClass, object>[] getters = DynamicType <TestClass> .CreateAllPropertyGetters();

            TestClass obj = new TestClass {
                Name = "AllName", Count = 100, Phone = 5555551212
            };

            Assert.AreEqual(9, getters.Length);
            Assert.AreEqual(getters[0](obj), "AllName");
            Assert.AreEqual(getters[1](obj), 100);
            Assert.AreEqual(getters[2](obj), (PhoneNumber)5555551212);
            Assert.IsNotNull(getters[3](obj));
            Assert.IsNull(getters[4](obj));
        }
Пример #2
0
        /// <summary>
        ///     Gets the data table.
        /// </summary>
        /// <param name="items">The list.</param>
        /// <returns></returns>
        public DataTable Create(IEnumerable <T> items)
        {
            Argument.Assert.IsNotNull(items, nameof(items));

            DataTable table = new DataTable();

            //Get all the PropertyInfo's once.
            PropertyInfo[] property = typeof(T).GetProperties();
            PropertyGetter <T, object>[] getters = DynamicType <T> .CreateAllPropertyGetters();

            for (int i = 0; i < property.Length; i++)
            {
                Type propertyType = property[i].PropertyType;
                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propertyType = Nullable.GetUnderlyingType(propertyType);
                }

                table.Columns.Add(new DataColumn(property[i].Name, propertyType));
            }

            //Add all the extra columns
            for (int i = 0; i < columns.Count; i++)
            {
                table.Columns.Add(columns[i].Name, columns[i].Type);
            }

            //Add all the rows to the DataTable.
            foreach (T item in items)
            {
                object[] row = new object[getters.Length + columns.Count];
                //Get all the property values.
                for (int i = 0; i < getters.Length; i++)
                {
                    row[i] = getters[i](item);
                }

                //Get all the extra column values.
                for (int i = 0; i < columns.Count; i++)
                {
                    row[getters.Length + i] = columns[i].ValueGetter(item);
                }

                table.Rows.Add(row);
            }

            return(table);
        }
Пример #3
0
        public void ReferenceTypeCreateAllGetters()
        {
            PropertyGetter <TestClass, object>[] getters = DynamicType <TestClass> .CreateAllPropertyGetters();

            TestClass obj = new TestClass();

            obj.Name  = "AllName";
            obj.Count = 100;
            obj.Phone = 5555551212;

            Assert.AreEqual(9, getters.Length);
            for (int i = 0; i < 8; i++)
            {
                TestPropertyGetter(getters[i], obj);
            }
        }