UnityExplorer/src/UI/CacheObject/CacheMethod.cs

57 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
2021-05-05 21:27:09 +10:00
using UnityExplorer.UI.Inspectors;
2021-05-05 21:27:09 +10:00
namespace UnityExplorer.UI.CacheObject
{
public class CacheMethod : CacheMember
{
public MethodInfo MethodInfo { get; internal set; }
public override Type DeclaringType => MethodInfo.DeclaringType;
public override bool CanWrite => false;
2021-05-05 21:27:09 +10:00
public override bool IsStatic => MethodInfo.IsStatic;
public override bool ShouldAutoEvaluate => false;
2021-04-30 21:34:50 +10:00
public override void SetInspectorOwner(ReflectionInspector inspector, MemberInfo member)
{
2021-04-30 21:34:50 +10:00
base.SetInspectorOwner(inspector, member);
Arguments = MethodInfo.GetParameters();
2021-05-04 20:10:46 +10:00
if (MethodInfo.IsGenericMethod)
GenericArguments = MethodInfo.GetGenericArguments();
}
2021-05-04 20:10:46 +10:00
protected override object TryEvaluate()
{
try
{
2021-05-04 20:10:46 +10:00
var methodInfo = MethodInfo;
if (methodInfo.IsGenericMethod)
methodInfo = MethodInfo.MakeGenericMethod(Evaluator.TryParseGenericArguments());
if (Arguments.Length > 0)
return methodInfo.Invoke(DeclaringInstance, Evaluator.TryParseArguments());
2021-05-04 20:10:46 +10:00
var ret = methodInfo.Invoke(DeclaringInstance, new object[0]);
2021-05-04 20:10:46 +10:00
HadException = false;
LastException = null;
return ret;
}
catch (Exception ex)
{
HadException = true;
LastException = ex;
2021-05-04 20:10:46 +10:00
return null;
}
}
protected override void TrySetValue(object value) => throw new NotImplementedException("You can't set a method");
}
}