-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameDebugger.cs
More file actions
155 lines (133 loc) · 5.83 KB
/
GameDebugger.cs
File metadata and controls
155 lines (133 loc) · 5.83 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Linq;
using System.Reflection;
namespace Reflection
{
public static class GameDebugger
{
public static void DumpObject(object obj, string objectName)
{
Console.WriteLine($"=== Дамп объекта: {objectName} ===");
if (obj == null)
{
Console.WriteLine("Ошибка: объект равен null");
Console.WriteLine("=== Конец дампа ===");
return;
}
Type objType = obj.GetType();
Console.WriteLine($"Тип: {objType.Name}");
var allFields = objType.GetFields(
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.FlattenHierarchy
);
var visibleFields = allFields.Where(field => !IsBackingField(field)).ToList();
if (visibleFields.Any())
{
Console.WriteLine("Поля:");
foreach (var field in visibleFields)
{
try
{
object value = field.GetValue(obj);
string valueStr = (value == null) ? "null" : value.ToString();
string accessModifier = field.IsPublic ? "" : "(private) ";
Console.WriteLine($" - {accessModifier}{field.Name} : {field.FieldType.Name} = {valueStr}");
}
catch (Exception ex)
{
Console.WriteLine($" - {field.Name} : {field.FieldType.Name} = <Ошибка доступа: {ex.Message}>");
}
}
}
var properties = objType.GetProperties(
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.FlattenHierarchy
);
if (properties.Any())
{
Console.WriteLine("Свойства:");
foreach (var prop in properties)
{
try
{
if (prop.CanRead)
{
object value = prop.GetValue(obj);
string valueStr = (value == null) ? "null" : value.ToString();
string accessInfo = prop.CanWrite ? "" : " (readonly)";
Console.WriteLine($" - {prop.Name} : {prop.PropertyType.Name} = {valueStr}{accessInfo}");
}
else
Console.WriteLine($" - {prop.Name} : {prop.PropertyType.Name} = <только запись>");
}
catch (Exception ex)
{
Console.WriteLine($" - {prop.Name} : {prop.PropertyType.Name} = <Ошибка доступа: {ex.Message}>");
}
}
}
Console.WriteLine("=== Конец дампа ===\n");
}
private static bool IsBackingField(FieldInfo field)
{
return field.Name.Contains("k__BackingField") || (field.Name.StartsWith("<") && field.Name.EndsWith(">k__BackingField"));
}
public static bool InvokeMethod(object obj, string methodName, params object[] parameters)
{
if (obj == null)
{
Console.WriteLine($"Ошибка: объект равен null, метод '{methodName}' не может быть вызван");
return false;
}
Type objType = obj.GetType();
var methods = objType.GetMethods(
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static
).Where(m => m.Name == methodName).ToArray();
if (methods.Length == 0)
{
Console.WriteLine($"Метод '{methodName}' не найден в типе {objType.Name}");
return false;
}
MethodInfo method = methods.FirstOrDefault();
try
{
ParameterInfo[] methodParams = method.GetParameters();
if (methodParams.Length != parameters.Length)
{
Console.WriteLine($"Метод '{methodName}' ожидает {methodParams.Length} параметров, получено {parameters.Length}");
return false;
}
object result = method.Invoke(obj, parameters);
if (method.ReturnType != typeof(void))
{
string resultStr = (result == null) ? "null" : result.ToString();
Console.WriteLine($"Метод '{methodName}' вернул: {resultStr}");
}
else
{
Console.WriteLine($"Метод '{methodName}' успешно выполнен");
}
return true;
}
catch (TargetInvocationException ex)
{
Console.WriteLine($"Ошибка при выполнении метода '{methodName}': {ex.InnerException?.Message ?? ex.Message}");
return false;
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка при вызове метода '{methodName}': {ex.Message}");
return false;
}
}
}
}