internal void Print(string name, string type, Object o)
{
if (o == null)
{
// E.g. inPrivate null SomeStruct
AddLine(B, "{0}@{1}#{2}", name, "null", type);
return;
}
// ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
if (o is TpmStructureBase)
{
string ss = type;
if (ss.StartsWith("I"))
{
// If the member is an interface, also print the type of entity that is being dumped
string intType = o.GetType().ToString();
intType = intType.Substring(intType.LastIndexOf('.') + 1);
type = intType;
}
// Print the name and type but not the contents (that is printed recursibley below)
AddLine(B, "{0}@-#{1}", name, type);
// Recurse
Indent++;
((TpmStructureBase)o).ToStringInternal(this);
Indent--;
return;
}
// ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
if (o is Enum)
{
var en = (Enum)o;
string s = Enum.Format(en.GetType(), en, "g");
s = s.Replace(',', '|');
// name Elem1|Elem2
AddLine(B, "{0}@{1}#{2}", name, s, type);
return;
}
if (o is ValueType)
{
//checked that this actually works with Int64, etc.
UInt64 valIs = Convert.ToUInt64(o);
var signedVal = (Int64)valIs;
string hexString = Convert.ToString(signedVal, 16);
// ReSharper disable once SpecifyACultureInStringConversionExplicitly
AddLine(B, "{0}@{1} (0x{2})#{3}", name, valIs.ToString(), hexString, type);
return;
}
// ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
if (o is Array)
{
var a = (Array)o;
Type elementType = o.GetType().GetElementType();
if (elementType == typeof (byte))
{
// Byte arrays as special -
string hexString = "0x" + Globs.HexFromByteArrayCompactNoSpaces((byte[])a);
string typeString = String.Format("byte[{0}]", a.Length);
AddLine(B, "{0}@{1}#{2}", name, hexString, typeString);
return;
}
// ReSharper disable once RedundantIfElseBlock
else
{
B.AppendFormat("{0}Array - {1}[{2}]\n", Spaces(), type, a.Length);
Indent++;
for (int j = 0; j < a.Length; j++)
{
Object elem = a.GetValue(j);
// ReSharper disable once SpecifyACultureInStringConversionExplicitly
Print(elem.GetType().ToString(), j.ToString(), elem);
}
Indent--;
return;
}
}
Globs.Throw<NotImplementedException>("Print: Unknown type " + o.GetType());
}