Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a List with a dynamic object type

How can I create a new List<T> where the T is a dynamic Type object.

I have

dynamic DyObj = new ExpandoObject();

if (condition1)
{
  DyObj.Required = true;
  DyObj.Message = "This is the first property being accessed through dynamic object";
}
if (condition2)
{
    DyObj.Required = false;
    DyObj.Message = "This is the second property....";
}
// and so on...

I want to create List<Dyobj> and assign all the messages to Dyobj based on conditions.

Follow up data from comments:

var DyObjectsList = new List<dynamic>; 
dynamic DyObj = new ExpandoObject(); 
if (condition1) { 
    DyObj.Required = true; 
    DyObj.Message = "Message 1"; 
    DyObjectsList.Add(DyObj); 
} 
if (condition2) { 
    DyObj.Required = false; 
    DyObj.Message = "Message 2"; 
    DyObjectsList.Add(DyObj); 
} 

interestingly all the objects in DyObjectsList are replaced with the values of the last assigned object.

like image 757
BumbleBee Avatar asked Jul 06 '11 17:07

BumbleBee


People also ask

How do you create a dynamic object?

You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.

How do you create a dynamic object in C++?

A dynamic object is created using a "new" operator that returns a pointer to the newly constructed object and is destructed by a "delete" operator. A pointer variable is used to hold the pointer to the object that is returned by the "new" operator.

Is a list dynamic?

And lists are also dynamic, meaning that you can add elements to the list or remove elements from a list completely. So the list can grow or shrink depending on how you use it.

Can we create object of list in C#?

A list in C#, just like lists in other programming languages, is a collection of items that can be indexed, searched, sorted and manipulated. Since C# is an object-oriented programming language, knowing how to create a list of objects is vital to solving problems. A list containing several objects of a class.


2 Answers

Just use dynamic as the argument:

var list = new List<dynamic>();
like image 85
Kirk Woll Avatar answered Oct 06 '22 21:10

Kirk Woll


It appears you might be a bit confused as to how the .Add method works. I will refer directly to your code in my explanation.

Basically in C#, the .Add method of a List of objects does not COPY new added objects into the list, it merely copies a reference to the object (it's address) into the List. So the reason every value in the list is pointing to the same value is because you've only created 1 new DyObj. So your list essentially looks like this.

DyObjectsList[0] = &DyObj; // pointing to DyObj
DyObjectsList[1] = &DyObj; // pointing to the same DyObj
DyObjectsList[2] = &DyObj; // pointing to the same DyObj

...

The easiest way to fix your code is to create a new DyObj for every .Add. Putting the new inside of the block with the .Add would accomplish this goal in this particular instance.

var DyObjectsList = new List<dynamic>; 
if (condition1) { 
    dynamic DyObj = new ExpandoObject(); 
    DyObj.Required = true; 
    DyObj.Message = "Message 1"; 
    DyObjectsList .Add(DyObj); 
} 
if (condition2) { 
    dynamic DyObj = new ExpandoObject(); 
    DyObj.Required = false; 
    DyObj.Message = "Message 2"; 
    DyObjectsList .Add(DyObj); 
} 

your resulting List essentially looks like this

DyObjectsList[0] = &DyObj0; // pointing to a DyObj
DyObjectsList[1] = &DyObj1; // pointing to a different DyObj
DyObjectsList[2] = &DyObj2; // pointing to another DyObj

Now in some other languages this approach wouldn't work, because as you leave the block, the objects declared in the scope of the block could go out of scope and be destroyed. Thus you would be left with a collection of pointers, pointing to garbage.

However in C#, if a reference to the new DyObjs exists when you leave the block (and they do exist in your List because of the .Add operation) then C# does not release the memory associated with that pointer. Therefore the Objects you created in that block persist and your List contains pointers to valid objects and your code works.

like image 7
Daniel Byrne Avatar answered Oct 06 '22 20:10

Daniel Byrne