【发布时间】:2015-05-05 20:39:16
【问题描述】:
假设我有两个类 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 类型的所有实例并检查 TheBar.Name 属性或 _whatever 字段 .NET 堆中存在 Foo 实例。我可以找到类型,但我不确定如何获取实例并查看其属性。有什么想法吗?
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?
}
}
}
}
【问题讨论】:
-
可以获取字段,但我认为不能获取属性,因为那无异于在附加过程中调用方法。我不认为 CLR MD 赋予你这样做的能力,因为目标甚至可能不是一个进程,而是一个转储文件。
-
你能举例说明我如何进入字段吗?我已更新我的问题以显示属性相关数据或字段