public void DynamicObjectPropertyNameAccessing()
        {
            dynamic sample = new DynamicObject();
            sample.TrySetMember("SimplePropertyName", "123");
            sample.TrySetMember("Property Name With Spaces", "456");
            sample.TrySetMember("PropertyNameWithNumbers123", "789");
            sample.TrySetMember("Property Name With Numbers And Spaces 123", "111");
            sample.TrySetMember("Property-Name-With-Dashes", "222");
            sample.TrySetMember("  PropertyNameWithLeadingTrailingSpaces  ", "333");
            sample.TrySetMember("EscapeCharactersTurnInUnderscores!@#", "444");

            // Demonstrates access of properties using the DLR
            Assert.AreEqual<string>("123", sample.SimplePropertyName);
            Assert.AreEqual<string>("456", sample.Property_Name_With_Spaces);
            Assert.AreEqual<string>("789", sample.PropertyNameWithNumbers123);
            Assert.AreEqual<string>("111", sample.Property_Name_With_Numbers_And_Spaces_123);
            Assert.AreEqual<string>("222", sample.Property_Name_With_Dashes);
            Assert.AreEqual<string>("333", sample.PropertyNameWithLeadingTrailingSpaces);
            Assert.AreEqual<string>("444", sample.EscapeCharactersTurnInUnderscores___);
        }
        /// <summary>
        /// Enumerates through rows of DataTable and returns dynamically-generated object for each row.
        /// </summary>
        public static IEnumerable<DynamicObject> EmitDynamicObjectFromDataTable(
                DataTable dataTable, int[] RowIndexIncludeFilter, int[] RowIndexExcludeFilter, 
                string[] CellValueIncludeFilterArray, string[] CellValueExcludeFilterArray)
        {
            // Keeps track of which row is being parsed for the sake of filtering
            // The first row always contains the column names, so we start with Index == 2
            int RowIndex = 2;

            // Cycle through each row of of table data
            foreach (System.Data.DataRow dataRow in dataTable.Rows)
            {
                bool FilterReject = false;

                // Row Exclusion Filtering: exclude anything in the array argument
                if (RowIndexExcludeFilter != null)
                {
                    if (RowIndexExcludeFilter.Contains(RowIndex))
                        FilterReject = true;
                }

                // Row Inclusion Filtering: when used, only include things in the array argument
                if (RowIndexIncludeFilter != null)
                {
                    if (!RowIndexIncludeFilter.Contains(RowIndex))
                        FilterReject = true;
                }

                // Cell Value Exclusion Filtering: exclude Row which contains a Column value that
                // matches any of the array
                if (CellValueExcludeFilterArray != null)
                {
                    foreach (DataColumn dataColumn in dataTable.Columns)
                    {
                        if (CellValueExcludeFilterArray.Contains(dataRow[dataColumn.ColumnName].ToString()))
                            FilterReject = true;
                    }
                }

                // Cell Value Inclusion Filtering: will *only* include Rows which contain at least one
                // of the values in the array argument
                if (CellValueIncludeFilterArray != null)
                {
                    bool CellValueFound = false;
                    foreach (DataColumn dataColumn in dataTable.Columns)
                    {
                        if (CellValueIncludeFilterArray.Contains(dataRow[dataColumn.ColumnName].ToString()))
                            CellValueFound = true;
                    }
                    if (CellValueFound != true)
                        FilterReject = true;
                }

                // Was this Row rejected by any of the filters?
                if (FilterReject == false)
                {
                    // Passed all the filters... now, create a Dynamic object for each row...
                    DynamicObject OutputDataRow = new DynamicObject();

                    // ... and cycle through each column and set the appropriate members
                    foreach (DataColumn dataColumn in dataTable.Columns)
                    {
                        OutputDataRow.TrySetMember(dataColumn.ColumnName, dataRow[dataColumn.ColumnName]);
                    }
                    yield return OutputDataRow;
                }

                ++RowIndex;
            }
        }
Example #3
0
        /// <summary>
        /// Enumerates through rows of DataTable and returns dynamically-generated object for each row.
        /// </summary>
        public static IEnumerable <DynamicObject> EmitDynamicObjectFromDataTable(
            DataTable dataTable, int[] RowIndexIncludeFilter, int[] RowIndexExcludeFilter,
            string[] CellValueIncludeFilterArray, string[] CellValueExcludeFilterArray)
        {
            // Keeps track of which row is being parsed for the sake of filtering
            // The first row always contains the column names, so we start with Index == 2
            int RowIndex = 2;

            // Cycle through each row of of table data
            foreach (System.Data.DataRow dataRow in dataTable.Rows)
            {
                bool FilterReject = false;

                // Row Exclusion Filtering: exclude anything in the array argument
                if (RowIndexExcludeFilter != null)
                {
                    if (RowIndexExcludeFilter.Contains(RowIndex))
                    {
                        FilterReject = true;
                    }
                }

                // Row Inclusion Filtering: when used, only include things in the array argument
                if (RowIndexIncludeFilter != null)
                {
                    if (!RowIndexIncludeFilter.Contains(RowIndex))
                    {
                        FilterReject = true;
                    }
                }

                // Cell Value Exclusion Filtering: exclude Row which contains a Column value that
                // matches any of the array
                if (CellValueExcludeFilterArray != null)
                {
                    foreach (DataColumn dataColumn in dataTable.Columns)
                    {
                        if (CellValueExcludeFilterArray.Contains(dataRow[dataColumn.ColumnName].ToString()))
                        {
                            FilterReject = true;
                        }
                    }
                }

                // Cell Value Inclusion Filtering: will *only* include Rows which contain at least one
                // of the values in the array argument
                if (CellValueIncludeFilterArray != null)
                {
                    bool CellValueFound = false;
                    foreach (DataColumn dataColumn in dataTable.Columns)
                    {
                        if (CellValueIncludeFilterArray.Contains(dataRow[dataColumn.ColumnName].ToString()))
                        {
                            CellValueFound = true;
                        }
                    }
                    if (CellValueFound != true)
                    {
                        FilterReject = true;
                    }
                }

                // Was this Row rejected by any of the filters?
                if (FilterReject == false)
                {
                    // Passed all the filters... now, create a Dynamic object for each row...
                    DynamicObject OutputDataRow = new DynamicObject();

                    // ... and cycle through each column and set the appropriate members
                    foreach (DataColumn dataColumn in dataTable.Columns)
                    {
                        OutputDataRow.TrySetMember(dataColumn.ColumnName, dataRow[dataColumn.ColumnName]);
                    }
                    yield return(OutputDataRow);
                }

                ++RowIndex;
            }
        }