如何填充Class对象以进行序列化和反序列化。 [英] How to populate the Class objects for serialization and Deserialization.

查看:73
本文介绍了如何填充Class对象以进行序列化和反序列化。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它构建对象结构并使用一些虚拟数据填充它。

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}
};









现在我必须填写此代码的// TODO部分,第一个TODO是关于的,填写项目类型的数据,其中对象可以类似于字符串或类别中的任何可用元素。

和第二个TODO是关于填充一种IWrapper的数组。



请帮我解决这个问题。我是C#的新手,明天我必须提交。下面是代码片段。



提前致谢。





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

推荐答案

请尝试使用我的文章: fastJSON [ ^ ]
Try using my article here : fastJSON[^]


这篇关于如何填充Class对象以进行序列化和反序列化。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆