-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetFieldValue.cs
More file actions
47 lines (38 loc) · 1.51 KB
/
GetFieldValue.cs
File metadata and controls
47 lines (38 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// *** Example: Attach to managed process and read object's field value ***
// Requires Microsoft.Diagnostics.Runtime (https://www.nuget.org/packages/Microsoft.Diagnostics.Runtime/)
//
// Author: MSDN-WhiteKnight (https://github.com/MSDN-WhiteKnight)
using System;
using Microsoft.Diagnostics.Runtime;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int pid = 1234;
DataTarget dt=DataTarget.AttachToProcess(pid,5000,AttachFlag.Passive);
using (dt)
{
//pick first CLR version
ClrInfo runtimeInfo = dt.ClrVersions[0];
ClrRuntime runtime = runtimeInfo.CreateRuntime();
ClrType type;
//enumerate objects on managed heap
foreach (ulong obj in runtime.Heap.EnumerateObjectAddresses())
{
type = runtime.Heap.GetObjectType(obj);
if (type == null) continue;
if (type.Name == "MyProject.MyClass")
{
Console.WriteLine("Address 0x{0:X}: {1}", obj, type.Name);
ClrInstanceField f = type.GetFieldByName("Foo");
object val = f.GetValue(obj);
if (val != null) Console.WriteLine(val.ToString());
}
}
}
Console.ReadKey();
}
}
}