mirror of
https://github.com/codewriter-packages/Tri-Inspector.git
synced 2025-01-22 00:08:51 -05:00
Add method resolvers
This commit is contained in:
parent
f819b0e46a
commit
7c1e995bdf
3
Editor/Resolvers.meta
Normal file
3
Editor/Resolvers.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca8a2709dde94e318cb5cfdc801d9045
|
||||
timeCreated: 1652095622
|
25
Editor/Resolvers/ErrorMethodResolver.cs
Normal file
25
Editor/Resolvers/ErrorMethodResolver.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
3
Editor/Resolvers/ErrorMethodResolver.cs.meta
Normal file
3
Editor/Resolvers/ErrorMethodResolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3b804d7029c42c0b5f5a692281dacbf
|
||||
timeCreated: 1652096547
|
56
Editor/Resolvers/InstanceMethodResolver.cs
Normal file
56
Editor/Resolvers/InstanceMethodResolver.cs
Normal 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>());
|
||||
}
|
||||
}
|
||||
}
|
3
Editor/Resolvers/InstanceMethodResolver.cs.meta
Normal file
3
Editor/Resolvers/InstanceMethodResolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cee79123a0e84d5d9795ea92ed1b6d9e
|
||||
timeCreated: 1652097174
|
23
Editor/Resolvers/MethodResolver.cs
Normal file
23
Editor/Resolvers/MethodResolver.cs
Normal 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);
|
||||
}
|
||||
}
|
3
Editor/Resolvers/MethodResolver.cs.meta
Normal file
3
Editor/Resolvers/MethodResolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 467299addfb7498fbb7e7f0e449c4364
|
||||
timeCreated: 1652095635
|
Loading…
Reference in New Issue
Block a user