Create custom object from ArrayList in C # at runtime

I have the following content stored in ArrayList as a pure string, each string represents the value of an element in the list, is there any way to generate a dynamic object in the following style: [left operand is a property] = [right operand is the value of this property]

object.data.details[1].tid=711
object.data.details[1].type="ongoing"
object.data.details[1].attach="node1"
object.data.details[1].flowing[1].comid="component1"
object.data.details[1].flowing[1].system-usage.RAM="12%"
object.data.details[1].flowing[1].system-usage.CPU="3%"
object.data.details[1].flowing[1].system-usage.Pages=3
object.data.details[1].flowing[2].comid="component2"
object.data.details[1].flowing[2].system-usage.RAM="9%"
object.data.details[1].flowing[2].system-usage.CPU="2%"
object.data.details[1].flowing[2].system-usage.Pages=1
object.data.details[1].flowing[3].comid="component3"
object.data.details[1].flowing[3].system-usage.RAM="7%"
object.data.details[1].flowing[3].system-usage.CPU="7%"
object.data.details[1].flowing[3].system-usage.Pages=5
object.data.details[2].tid=712
object.data.details[2].type="ongoing"
object.data.details[2].attach="node2"
object.data.details[2].flowing[1].comid="component1"
object.data.details[2].flowing[1].system-usage.RAM="12%"
object.data.details[2].flowing[1].system-usage.CPU="3%"
object.data.details[2].flowing[1].system-usage.Pages=3
object.data.details[2].flowing[2].comid="component2"
object.data.details[2].flowing[2].system-usage.RAM="9%"
object.data.details[2].flowing[2].system-usage.CPU="2%"
object.data.details[2].flowing[2].system-usage.Pages=1
object.data.details[2].flowing[3].comid="component3"
object.data.details[2].flowing[3].system-usage.RAM="7%"
object.data.details[2].flowing[3].system-usage.CPU="7%"
object.data.details[2].flowing[3].system-usage.Pages=5
object.data.details[3].tid=713
object.data.details[3].type="ongoing"
object.data.details[3].attach="node2"
object.data.details[3].flowing[1].comid="component1"
object.data.details[3].flowing[1].system-usage.RAM="12%"
object.data.details[3].flowing[1].system-usage.CPU="3%"
object.data.details[3].flowing[1].system-usage.Pages=3
object.data.details[3].flowing[2].comid="component2"
object.data.details[3].flowing[2].system-usage.RAM="9%"
object.data.details[3].flowing[2].system-usage.CPU="2%"
object.data.details[3].flowing[2].system-usage.Pages=1
object.data.details[3].flowing[3].comid="component3"
object.data.details[3].flowing[3].system-usage.RAM="7%"
object.data.details[3].flowing[3].system-usage.CPU="7%"
object.data.details[3].flowing[3].system-usage.Pages=5

      

I want to have an object with the following structure:

object.data.details[i].flowing[j].comid="component1"
if i=j=1
object.data.details[i].flowing[j].system-usage.RAM="9%"
if i=1 and j=2

      

In other words, I need to create a dynamic object that can have an arraylist as a list of properties, as if we could access any value displayed in the asp.net gridview using:

gridview1.rows[i].someproperty[j].value = "something"

      

I tried using ExpandoObject but failed and tried with reflection, but it consumes a lot of memory if I generate ~ 1K objects in one thread.

+3


source to share


1 answer


Ok guys, I figured out how to do this and just in case someone facing this issue would be the solution:

  • you have to tokenize each line using a dot (.) and a pointer to keep track of the index, you have to use a pointer with the ref keyword to jump over the SQUARED_OPEN and SQUARED_CLOSED characters and the index between

  • you will need the following object

    ExpandoObject expandoObject = new ExpandoObject ();
    IDictionary objects = expandoObject;
    
  • you need to create a recursive method that can add the tokenized first string (first token) as a dictionary and its value as an expando object.

  • Add the rest of the line to each line as a value for expando

  • Mark the remaining line, which is already stored as an expando object, then repeat 3,4



that's all. you will have an expando object that will allow you to access any value by binding properties like:

<pre>
var x = data.details[1].flowing[2].system_usage.Pages;
</pre>

      

0


source







All Articles