protected void InternalWriteXml(XmlWriter w)
{
MemberInfo[] fields = GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
string thisObjectName = GetType().Name;
foreach (MemberInfo f in fields)
{
var mInfo = Globs.GetAttr<MarshalAsAttribute>(f);
Object obj = Globs.GetMember(f, this);
string elementName = f.Name;
elementName = elementName.TrimStart(new[] { '_' });
w.WriteStartElement(elementName);
switch(mInfo.MarshType)
{
case MarshalType.UnionSelector:
case MarshalType.ArrayCount:
// ReSharper disable once PossibleNullReferenceException
w.WriteString(obj.ToString());
break;
case MarshalType.Normal:
// ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
if(obj is TpmStructureBase)
{
//w.WriteStartElement(GetTypeName(obj));
((TpmStructureBase)obj).InternalWriteXml(w);
//w.WriteEndElement();
}
else
{
// ReSharper disable once PossibleNullReferenceException
w.WriteString(obj.ToString());
}
break;
case MarshalType.FixedLengthArray:
case MarshalType.VariableLengthArray:
// ReSharper disable once PossibleNullReferenceException
Type elementType = obj.GetType().GetElementType();
var supportedElementaryTypes = new[] { typeof(byte), typeof(ushort), typeof(uint) };
if (supportedElementaryTypes.Contains(elementType))
{
w.WriteValue(obj);
}
else
{
// ReSharper disable once UnusedVariable
foreach (Object o in (Array)obj)
{
// ReSharper disable once PossibleInvalidCastException
((TpmStructureBase)obj).InternalWriteXml(w);
}
}
break;
case MarshalType.Union:
// ReSharper disable once PossibleNullReferenceException
string typeName = obj.GetType().ToString();
w.WriteAttributeString("type", typeName);
//w.WriteStartElement(GetTypeName(obj));
((TpmStructureBase)obj).InternalWriteXml(w);
//w.WriteEndElement();
break;
default:
throw new NotImplementedException("NOT IMPLEMENTED");
}
w.WriteEndElement();
}
}