Click here to Skip to main content
14,767,353 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I have Below code, which builds the object structure and populates it using some dummy data like so.
object[] dataMap = new object[]
{
   (byte) 1,
   new byte[] {1,0},

   (int) 1,
   new int[]{1,0},

   (double) 1.111111,
   new double[] {1.1111, 2.222222},

   (float) 1.111,
   new float[] {(float)1.111, (float)2.222},

   (bool) false,
   new bool[] {false, true},

   (string) "ABCD",
   new string[]{"ABCD", "WXYZ"},

   (DateTime) DateTime.Now,
   new DateTime[] {DateTime.Now, DateTime.Today}
};




Now I have to fill up the //TODO parts of this code, Where the 1st TODO is about, Filling in the data for item types, where the object can resemble any of the elements avaliable like string or a class.
and the 2nd TODO is about populating an array of a type of IWrapper.

Please help me out on this. I am new to C# and I have to submit this tomorrow. Below is the code snippet.

Thanks in advance.
/// <summary>
/// Builds all the nodes of an object structure.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private object[] BuildObjectStructure(object obj)
{
   object[] retVal = new object[] { obj };

   Type t = obj.GetType();

   try
   {
      foreach (PropertyInfo pi in t.GetProperties())
      {
         if (pi.Name != "RawData")
         {
            bool assigned = false;
            foreach(object data in dataMap)
            {
               if(data.GetType().Equals(pi.PropertyType))
               {
                  pi.SetValue(obj, data, null);
                  assigned = true;
                  break;
               }
            }

            if (!assigned)
            {
            //TODO: Keep provision to identify the mapped object structure and for mapped type
            //clone the structure and then create variants.
               if (pi.PropertyType.IsArray)
               {
                  //TODO: Handle array in a different way where a single element array can
                  //be created and assigned explicitly or maybe code can be update to handle
                  //this so that tests can be easier.

                  Type typ = pi.PropertyType.GetElementType();

                  if (typ.IsInterface)
                  {
                     //if (pi.Name != "RawData")
                     //{

                     //    object[] objI = new object[1];
                     //    objI[0] = pi.GetValue((typ.BaseType), null);// pi.GetValue(obj, null);
                     //    BuildObjectStructure((object)objI[0]);
                     //}

                     object[] objI = new object[1];
                     objI[0] = Activator.CreateInstance(typ);
                     //var attributes = fooObject.GetAttributes<XmlChoiseAttribute>();

                     //foreach (var attribute in attributes)
                     //{
                     //    //here you have access to the attributes
                     //    //and you can do whatever you want with them
                     //}

                     BuildObjectStructure((object)objI[0]);
                  }
                  else
                  {
                     object[] objI = new object[1];
                     objI[0] = Activator.CreateInstance(typ);
                     BuildObjectStructure((object)objI[0]);
                  }
               }
               else
               {
                  object val = pi.GetValue(obj, null);
                  if (val != null)
                  {
                     BuildObjectStructure(val);
                  }
               }
            }
         }
      }
   }
   catch (Exception e)
   {
      //TODO: Do error handling as exception is not really expected here. Something must have terribly gone wrong. May want to fail test.
   }

   return retVal;
}


Unnecessary code tags removed indentation and empty lines reduced
Posted
Updated 11-May-14 7:34am
v3

1 solution

Try using my article here : fastJSON[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900