Build an object structure for Serilaization and DeSerilaizat

May 11 2014 5:25 PM
-

I have a below code which builds the object structure, that is fills in some dummy data for serilaization and deserialization. There are few "XmlElementAttribute" elements in classes which can have multiple types.

Now, I have to wrtie code for a type to fill in data for one choice type element at one instance and the other at other instances. The below TODO Comment specifies where i have to code for this. Here, I have choice type elelments in the schema like This is the code automatically generated from a Schema definition. "
 
 
 
 /// [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] public partial class ClassWithObjectTypeMappedToDifferentTypeProperties { private object itemField;
 
    /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("PrimitiveNormal1", typeof(byte))] [System.Xml.Serialization.XmlElementAttribute("PrimitiveNormal2", typeof(string))] [System.Xml.Serialization.XmlElementAttribute("PrimitiveArray1", typeof(byte[]))] [System.Xml.Serialization.XmlElementAttribute("PrimitiveArray2", typeof(string[]))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedNormal1", typeof(UserDefinedType1))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedNormal2", typeof(UserDefinedType2))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedArray1", typeof(UserDefinedType1[]))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedArray2", typeof(UserDefinedType2[]))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedEnumNormal1", typeof(UserDefinedEnumType1))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedEnumNormal2", typeof(UserDefinedEnumType2))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedEnumArray1", typeof(UserDefinedEnumType1[]))] [System.Xml.Serialization.XmlElementAttribute("UserDefinedEnumArray2", typeof(UserDefinedEnumType2[]))] public object Item { get { return this.itemField; } set { this.itemField = value; } }}

" "//TODO: Keep provision to identify the mapped object structure and for mapped type //clone the structure and then create variants."

2nd thing, How I can create one instance for Array types and fills in data for it. The Following "TODO"comment provides the place holder for this.

"//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. "

Please help me out one this, I have to submit this urgently...

Thanks in advance...

Below is the Code :
 

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;

}