1

Let’s say list of member names stored in an array like below,

string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };

I wrote the following code to generate dynamic class and members along with random values.

var myDynamicClassList = new List<ExpandoObject>();
        foreach (var MemberName in myMembers)
        {
            dynamic dynamicClass = new ExpandoObject();
            dynamicClass.MemberName = new Random().Next();
            myDynamicClassList.Add(dynamicClass);
        }

In order to display output of the above myDynamicClassList I wrote the following code.

foreach (var x in myDynamicClassList)
        {
            foreach (var property in (IDictionary<String, Object>)x)
            {
                Console.WriteLine(property.Key + ": " + property.Value);
            }
        }

Showing output like this

MemberName : 123465461

MemberName : 564613611

MemberName : 134654321

MemberName : 786451214

But Instead of above output I am expecting the output like below

ChapterName : 123465461

Medium : 564613611

LastName : 134654321

OrderID : 786451214

Here is my question, is it possible to add dynamic member name to a dynamic class in c#. If it is possible please let me know, if not please guide me to complete this job.

I really appreciate your help in advanced.

Community
  • 1
  • 1
sridharnetha
  • 2,104
  • 8
  • 35
  • 69

2 Answers2

1

As described in a similar question ("Dynamically adding properties to an ExpandoObject"), you can do this by using IDictionary directly:

string[] myMembers = { "ChapterName", "Medium", "LastName", "OrderID" };
var myDynamicClassList = new List<ExpandoObject>();
Random random = new Random();
foreach (var MemberName in myMembers)
{
    IDictionary<string, object> dynamicClass = (IDictionary<string, object>)(new ExpandoObject());
    dynamicClass.Add(MemberName, random.Next());
    myDynamicClassList.Add((ExpandoObject)dynamicClass);
}
foreach (var x in myDynamicClassList)
{
    foreach (var property in (IDictionary<String, Object>)x)
    {
        Console.WriteLine(property.Key + ": " + property.Value);
    }
}

However, you should be aware of possible known memory limitations with this method as described in the comments of the post linked above, or in the Microsoft issue. I would possibly rethink the design to consider what properties are actually needed, or simply using a Dictionary if it's feasible (to avoid the dynamic ExpandoObject entirely).

I also moved your Random outside of the loop. Creating new instances of Random inside a loop may not give you the results you expect...

Community
  • 1
  • 1
grovesNL
  • 6,016
  • 2
  • 20
  • 32
  • 1
    It is not necessary to modify your `new List();` because `ExpandoObject` implements `IDictionary`. You will not have any issues compiling or running this code. – grovesNL Mar 22 '15 at 07:09
  • 1
    Thank you for your answer. I got overloaded error in previous code at line `myDynamicClassList.Add(dynamicClass);` so i have modified code from `var myDynamicClassList = new List();` to `var myDynamicClassList = new List>();` Yes i agree with you alternate solutions is type casting like modifying the code from `myDynamicClassList.Add(dynamicClass);` to `myDynamicClassList.Add((ExpandoObject)dynamicClass);` – sridharnetha Mar 22 '15 at 07:19
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;

namespace Deligate
{
    class Program
    {
       public static List<Employee> empList = new List<Employee>();
        public static void UserInfo(int val)
        {
            Employee emp = new Employee();
            Console.WriteLine($"Enter {val+1} Employee Details :- ");
            Console.WriteLine("Enter Your Name: ");
            emp.Name= Console.ReadLine();
            Console.WriteLine("Enter your Email: ");
            emp.Email = Console.ReadLine();
            Console.WriteLine("Enter Mobile Number");
            emp.Mobile = Console.ReadLine();
            empList.Add(emp);
           
        }
        public static void CountEmp()
        {
            
            Console.WriteLine("How many user you want to add:");
            var UserCount = int.Parse(Console.ReadLine());

            for (int i = 0; i < UserCount; i++)
            {
                UserInfo(i);
            }
            foreach (var employee in empList) {
                Console.WriteLine(employee.Name+ ", " + employee.Email+", "+employee.Mobile);
                
            }
            System.IO.File.Delete(@"D:\Compare.txt");

            foreach (var el in empList)
            {
               System.IO.File.AppendAllText(@"D:\Compare.txt", el.Name+", "+el.Email+", "+el.Mobile + Environment.NewLine);
                
            }
        }
         public static void Main(string[] args)
        {
            CountEmp();
            Console.ReadKey();
           
        }
    }
}