Files

83 lines
2.6 KiB
C#
Raw Permalink Normal View History

2023-07-01 21:07:29 -05:00
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using UnityEngine;
namespace SmartPoint.AssetAssistant.UnityExtensions
{
[Serializable]
public struct SerializedMethod
{
2023-07-14 14:42:33 -05:00
public string AssemblyQualifiedName { get; set; }
public string MethodName { get; set; }
public bool IsStatic { get; set; }
public MethodInfo Method { get; set; }
2023-07-01 21:07:29 -05:00
2023-07-14 14:42:33 -05:00
//public SerializedMethod(string assemblyQualifiedName, string methodName, bool isStatic, MethodInfo method)
2023-07-01 21:07:29 -05:00
//{
2023-07-14 14:42:33 -05:00
//this.AssemblyQualifiedName = assemblyQualifiedName;
//this.MethodName = methodName;
//this.IsStatic = isStatic;
//this.Method = method;
2023-07-01 21:07:29 -05:00
//}
//public SerializedMethod(MethodInfo methodInfo)
//{
2023-07-14 14:42:33 -05:00
//this.Method = methodInfo;
//this.AssemblyQualifiedName = methodInfo.ReflectedType.AssemblyQualifiedName;
//this.MethodName = methodInfo.Name;
//this.IsStatic = methodInfo.IsStatic;
2023-07-01 21:07:29 -05:00
//}
public void Invoke()
{
2023-07-14 14:42:33 -05:00
var methodInfo = GetMethodInfo();
if (methodInfo != null)
{
methodInfo.Invoke(null, null);
}
2023-07-01 21:07:29 -05:00
}
public void Invoke(object obj, object[] parameters)
{
2023-07-14 14:42:33 -05:00
var methodInfo = GetMethodInfo();
if (methodInfo != null)
{
methodInfo.Invoke(obj, parameters);
}
2023-07-01 21:07:29 -05:00
}
2023-07-14 14:42:33 -05:00
private MethodInfo GetMethodInfo()
2023-07-01 21:07:29 -05:00
{
2023-07-14 14:42:33 -05:00
if (!string.IsNullOrEmpty(this.AssemblyQualifiedName) && !string.IsNullOrEmpty(this.MethodName))
{
var type = Type.GetType(this.AssemblyQualifiedName) ?? Type.GetType(this.AssemblyQualifiedName + ", Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
if (type != null)
{
var bindingFlags = this.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
return type.GetMethod(this.MethodName, bindingFlags | BindingFlags.Public | BindingFlags.NonPublic);
}
}
2023-07-01 21:07:29 -05:00
return null;
}
public RuntimeMethodHandle GetMethod()
{
2023-07-14 14:42:33 -05:00
var methodInfo = GetMethodInfo();
return methodInfo != null ? methodInfo.MethodHandle : default;
2023-07-01 21:07:29 -05:00
}
[SerializeField]
private string _assemblyQualifiedName;
[SerializeField]
private string _methodName;
[SerializeField]
private bool _isStatic;
}
}