There is a helper class named SciTech.Profiler.InstanceDataRetriever that can be used to parse the instance data of an instance in a profiler snapshot. The example code below shows how field information can be retrieved.
- Code: Select all
var retriever = new InstanceDataRetriever(comparison);
int maxFields = 1000;
RetrievedFieldDataCollection fields = retriever.RetrieveFields(instanceId, maxFields);
if( fields.HasMissingFields )
{
// Fields may be missing under some circumstances, e.g. uninitialized nested value types.
Console.WriteLine("One or more fields are missing in the collection.");
}
if( fields.IsTruncated)
{
// Arrays and strings may be truncated if the number of items exceeds maxFields
Console.WriteLine("Fields collection is truncated.");
}
foreach( var field in fields )
{
Console.WriteLine($"{field.Field.Name}: {field.Value}");
if( field.Value is TypeInstanceId )
{
Console.WriteLine(" Field is a reference field.");
}
if( field.SubFields.Count > 0 )
{
Console.WriteLine(" Instance has nested value type fields.");
}
}
Using PtrToStructure to retrieve instance data is not safe, since the instance data layout is often different from the .NET marshalling layout. However, the fields information retrieved by InstanceDataRetriever is more suitable for presentation than programmatic analysis. I will try to write an sample that uses the InstanceDataRetriever to unmarshal the instance data to an actual instance of the type, similar to the PtrToStructure method.