locked
Dynamically extending properties in C# RRS feed

  • Question

  • User-1340937076 posted

    Hi,

    I want to build anonymous type object dynamically with some additional properties. I have product screen in my application and user can add/ remove products from the ProductList screen.

    I have another screen where the column names are products and they can extend. I want to create Model (class) which will contain newly added/ deleted products as property so that I can build object and return list of the items.

    class MyClass{

                public string MilestoneID { get; set; }
                public string Product1 { get; set; }
                public string Product2 { get; set; }
                public string Product3 { get; set; }

                --------------------

                ----------------------

    }

    It looks like; I will need to use Reflection to create dynamic properties but NOT sure how to do that using exsting class

    Please let me know if you have any sample code which will help me quickly.

    Friday, September 7, 2012 8:48 AM

Answers

  • User-1340937076 posted

    DataTable has resolved my issue and used following code to identify columns -

    DataTable table = new DataTable();

                // Declare DataColumn and DataRow variables.
                DataColumn column;
                DataRow row;
                
                // Create new DataColumn, set DataType, ColumnName and add to DataTable.    
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "id";
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "first";
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "last";
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "email";
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "address";
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = Type.GetType("System.String");
                column.ColumnName = "Phone";
                table.Columns.Add(column);

                // Create new DataRow objects and add to DataTable.     
                for (int i = 1; i < 10; i++)
                {
                    row = table.NewRow();
                    row["id"] = i;
                    row["first"] = "first " + i.ToString();
                    row["last"] = "last " + i.ToString();
                    row["email"] = "Email " + i.ToString();
                    row["address"] = "Address " + i.ToString();
                    row["Phone"] = "Phone " + i.ToString();
                    table.Rows.Add(row);
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 7, 2012 9:30 AM