Add method resolvers

This commit is contained in:
VladV 2022-05-09 15:06:51 +03:00
parent f819b0e46a
commit 7c1e995bdf
7 changed files with 116 additions and 0 deletions

3
Editor/Resolvers.meta Normal file
View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ca8a2709dde94e318cb5cfdc801d9045
timeCreated: 1652095622

View File

@ -0,0 +1,25 @@
using System;
namespace TriInspector.Resolvers
{
internal class ErrorMethodResolver<TReturnType> : MethodResolver<TReturnType>
{
private readonly string _method;
public ErrorMethodResolver(TriPropertyDefinition propertyDefinition, string method)
{
_method = method;
}
public override bool TryGetErrorString(out string error)
{
error = $"Method '{_method}' not exists or has wrong signature";
return true;
}
public override TReturnType InvokeForTarget(TriProperty property, int targetIndex)
{
throw new InvalidOperationException("Method not exists");
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b3b804d7029c42c0b5f5a692281dacbf
timeCreated: 1652096547

View File

@ -0,0 +1,56 @@
using System;
using System.Reflection;
namespace TriInspector.Resolvers
{
internal class InstanceMethodResolver<TReturnType> : MethodResolver<TReturnType>
{
private readonly MethodInfo _methodInfo;
public static bool TryResolve(TriPropertyDefinition propertyDefinition, string method,
out MethodResolver<TReturnType> resolver)
{
var parentType = propertyDefinition.MemberInfo.DeclaringType;
if (parentType == null)
{
resolver = null;
return false;
}
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
foreach (var methodInfo in parentType.GetMethods(flags))
{
if (methodInfo.Name == method &&
methodInfo.ReturnType == typeof(TReturnType) &&
methodInfo.GetParameters() is var parameterInfos &&
parameterInfos.Length == 0)
{
resolver = new InstanceMethodResolver<TReturnType>(methodInfo);
return true;
}
}
resolver = null;
return false;
}
private InstanceMethodResolver(MethodInfo methodInfo)
{
_methodInfo = methodInfo;
}
public override bool TryGetErrorString(out string error)
{
error = "";
return false;
}
public override TReturnType InvokeForTarget(TriProperty property, int targetIndex)
{
var parentValue = property.Parent.GetValue(targetIndex);
return (TReturnType) _methodInfo.Invoke(parentValue, Array.Empty<object>());
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cee79123a0e84d5d9795ea92ed1b6d9e
timeCreated: 1652097174

View File

@ -0,0 +1,23 @@
namespace TriInspector.Resolvers
{
public static class MethodResolver
{
public static MethodResolver<TReturnType> Resolve<TReturnType>(TriPropertyDefinition propertyDefinition,
string method)
{
if (InstanceMethodResolver<TReturnType>.TryResolve(propertyDefinition, method, out var imr))
{
return imr;
}
return new ErrorMethodResolver<TReturnType>(propertyDefinition, method);
}
}
public abstract class MethodResolver<TReturnType>
{
public abstract bool TryGetErrorString(out string error);
public abstract TReturnType InvokeForTarget(TriProperty property, int targetIndex);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 467299addfb7498fbb7e7f0e449c4364
timeCreated: 1652095635