Tri-Inspector/Editor/Resolvers/InstanceFieldValueResolver.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2022-05-11 12:48:50 -04:00
using System;
using System.Reflection;
using UnityEngine;
namespace TriInspector.Resolvers
{
internal sealed class InstanceFieldValueResolver<T> : ValueResolver<T>
{
private readonly FieldInfo _fieldInfo;
public static bool TryResolve(TriPropertyDefinition propertyDefinition, string expression,
out ValueResolver<T> resolver)
{
2022-08-17 13:46:26 -04:00
var parentType = propertyDefinition.OwnerType;
2022-05-11 12:48:50 -04:00
if (parentType == null)
{
resolver = null;
return false;
}
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
foreach (var fieldInfo in parentType.GetFields(flags))
{
if (fieldInfo.Name == expression &&
typeof(T).IsAssignableFrom(fieldInfo.FieldType))
2022-05-11 12:48:50 -04:00
{
resolver = new InstanceFieldValueResolver<T>(fieldInfo);
return true;
}
}
resolver = null;
return false;
}
private InstanceFieldValueResolver(FieldInfo fieldInfo)
{
_fieldInfo = fieldInfo;
}
public override bool TryGetErrorString(out string error)
{
error = "";
return false;
}
public override T GetValue(TriProperty property, T defaultValue = default)
{
var parentValue = property.Parent.GetValue(0);
try
{
return (T) _fieldInfo.GetValue(parentValue);
}
catch (Exception e)
{
if (e is TargetInvocationException targetInvocationException)
{
e = targetInvocationException.InnerException;
}
Debug.LogException(e);
return defaultValue;
}
}
}
}