@@ -8,34 +8,64 @@ public static class ReflectionHelper
|
||||
{
|
||||
private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
|
||||
// --- 设置值 (支持 object) ---
|
||||
// --- 设置值 (支持 object, 支持 List/Array 下标) ---
|
||||
public static void SetDeepValue(object target, string path, object newValue)
|
||||
{
|
||||
if (target == null || string.IsNullOrEmpty(path)) return;
|
||||
string[] parts = path.Split('.');
|
||||
SetDeepValueRecursive(target, parts, 0, newValue);
|
||||
SetDeepValueRecursiveWithList(target, parts, 0, newValue);
|
||||
}
|
||||
|
||||
private static void SetDeepValueRecursive(object currentObj, string[] pathParts, int index, object newValue)
|
||||
private static void SetDeepValueRecursiveWithList(object currentObj, string[] pathParts, int index, object newValue)
|
||||
{
|
||||
string memberName = pathParts[index];
|
||||
Type type = currentObj.GetType();
|
||||
int idx;
|
||||
bool isIndex = int.TryParse(memberName, out idx);
|
||||
bool isLast = index == pathParts.Length - 1;
|
||||
|
||||
FieldInfo field = type.GetField(memberName, Flags);
|
||||
PropertyInfo prop = type.GetProperty(memberName, Flags);
|
||||
|
||||
if (field == null && prop == null)
|
||||
if (isIndex)
|
||||
{
|
||||
// Debug.LogWarning($"找不到成员: {memberName}"); // 可选:调试用
|
||||
// List/Array 下标
|
||||
if (currentObj is System.Collections.IList list && idx >= 0 && idx < list.Count)
|
||||
{
|
||||
if (isLast)
|
||||
{
|
||||
list[idx] = newValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
object nextObj = list[idx];
|
||||
SetDeepValueRecursiveWithList(nextObj, pathParts, index + 1, newValue);
|
||||
// Struct 回写
|
||||
if (nextObj != null && nextObj.GetType().IsValueType)
|
||||
list[idx] = nextObj;
|
||||
}
|
||||
}
|
||||
else if (type.IsArray && idx >= 0 && idx < ((Array)currentObj).Length)
|
||||
{
|
||||
Array arr = (Array)currentObj;
|
||||
if (isLast)
|
||||
{
|
||||
arr.SetValue(newValue, idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
object nextObj = arr.GetValue(idx);
|
||||
SetDeepValueRecursiveWithList(nextObj, pathParts, index + 1, newValue);
|
||||
if (nextObj != null && nextObj.GetType().IsValueType)
|
||||
arr.SetValue(nextObj, idx);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
bool isLast = index == pathParts.Length - 1;
|
||||
FieldInfo field = type.GetField(memberName, Flags);
|
||||
PropertyInfo prop = type.GetProperty(memberName, Flags);
|
||||
if (field == null && prop == null) return;
|
||||
|
||||
if (isLast)
|
||||
{
|
||||
// 到达终点:直接设置值
|
||||
// 注意:这里可能需要处理类型转换,取决于你的 newValue 类型是否严格匹配
|
||||
try
|
||||
{
|
||||
if (field != null) field.SetValue(currentObj, newValue);
|
||||
@@ -45,13 +75,9 @@ public static class ReflectionHelper
|
||||
}
|
||||
else
|
||||
{
|
||||
// 中间节点
|
||||
object nextObj = field != null ? field.GetValue(currentObj) : prop.GetValue(currentObj);
|
||||
if (nextObj == null) return;
|
||||
|
||||
SetDeepValueRecursive(nextObj, pathParts, index + 1, newValue);
|
||||
|
||||
// Struct 回写逻辑 (关键)
|
||||
SetDeepValueRecursiveWithList(nextObj, pathParts, index + 1, newValue);
|
||||
if (nextObj.GetType().IsValueType)
|
||||
{
|
||||
if (field != null) field.SetValue(currentObj, nextObj);
|
||||
@@ -60,7 +86,7 @@ public static class ReflectionHelper
|
||||
}
|
||||
}
|
||||
|
||||
// --- 获取值 (支持 object) ---
|
||||
// --- 获取值 (支持 object, 支持 List/Array 下标) ---
|
||||
public static object GetDeepValue(object target, string path)
|
||||
{
|
||||
object current = target;
|
||||
@@ -69,6 +95,27 @@ public static class ReflectionHelper
|
||||
if (current == null) return null;
|
||||
Type type = current.GetType();
|
||||
|
||||
// 检查是否为下标访问
|
||||
int idx;
|
||||
if (int.TryParse(part, out idx))
|
||||
{
|
||||
// List/Array 下标
|
||||
if (current is System.Collections.IList list && idx >= 0 && idx < list.Count)
|
||||
{
|
||||
current = list[idx];
|
||||
continue;
|
||||
}
|
||||
else if (type.IsArray && idx >= 0 && idx < ((Array)current).Length)
|
||||
{
|
||||
current = ((Array)current).GetValue(idx);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null; // 下标无效
|
||||
}
|
||||
}
|
||||
|
||||
FieldInfo field = type.GetField(part, Flags);
|
||||
if (field != null)
|
||||
{
|
||||
@@ -86,6 +133,7 @@ public static class ReflectionHelper
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 遍历对象的所有字段和属性(递归地),查找值与目标值匹配的参数路径。
|
||||
/// </summary>
|
||||
@@ -193,6 +241,16 @@ public static class ReflectionHelper
|
||||
FindRecursive(value, targetValue, newPath, depth + 1, results, visited, maxDepth);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找对象中第一个与目标值相等的字段/属性路径(可直接用于 SetDeepValue)。找不到则返回 null。
|
||||
/// </summary>
|
||||
public static string FindFirstParameterPathByValue(object target, object targetValue, int maxDepth = 10)
|
||||
{
|
||||
if (target == null || targetValue == null) return null;
|
||||
var results = FindParametersByValue(target, targetValue, maxDepth);
|
||||
return results.Count > 0 ? results[0] : null;
|
||||
}
|
||||
}
|
||||
public class FastReflection
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user