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