假设我有两个类 Foo 和 Bar 如下
public class Foo
{
private Bar _bar;
private string _whatever = "whatever";
public Foo()
{
_bar = new Bar();
}
public Bar TheBar
{
get
{
return _bar;
}
}
}
public class Bar
{
public string Name { get; set; }
}
我有一个附加到使用这些类的进程的应用程序。我想在 .NET 堆中查看 Foo 类型的所有实例,并检查 .NET 堆中存在的所有 Foo 实例的 TheBar.Name 属性或 _whatever 字段。我可以找到类型,但我不确定如何获取实例并查看其属性。有什么想法吗?
using (DataTarget target = DataTarget.AttachToProcess(processId, 30000))
{
string dacLocation = target.ClrVersions[0].TryGetDacLocation();
ClrRuntime runtime = target.CreateRuntime(dacLocation);
if (runtime != null)
{
ClrHeap heap = runtime.GetHeap();
foreach (ulong obj in heap.EnumerateObjects())
{
ClrType type = heap.GetObjectType(obj);
if (type.Name.Compare("Foo") == 0 )
{
// I would like to see value of TheBar.Name property or _whatever field of all instances of Foo type in the heap. How can I do it?
}
}
}
}