网上演练贵港万达广场(人员密集)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 lines
2.6 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public static class ReflectionExtensions
{
public static void InvokeMethod(this object obj, string method, object value = null)
{
if (obj == null)
throw new NullReferenceException("Target Object is null.");
Type objType = obj.GetType();
object[] argValues = null;
Type[] argTypes = null;
if (value == null)
{
argValues = new object[0];
argTypes = new Type[0];
}
else
{
argValues = new object[] { value };
argTypes = new Type[] { value.GetType() };
}
InvokeMethod(obj, objType, method, argValues, argTypes);
}
public static void InvokeMethod(this object obj, string method, object[] argValues, Type[] argTypes)
{
InvokeMethod(obj, obj.GetType(), method, argValues, argTypes);
}
private static void InvokeMethod(object obj, Type objectType, string method, object[] argValues, Type[] argTypes)
{
#if !NETFX_CORE
BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.OptionalParamBinding;
MethodInfo methodInfo = null;
// 基于方法名称和参数类型查找
if (argValues != null)
methodInfo = objectType.GetMethod(method, bindingAttr, null, argTypes, null);
else
methodInfo = objectType.GetMethod(method, bindingAttr);
if (methodInfo != null)
methodInfo.Invoke(obj, argValues);
else if (objectType.BaseType != null)
InvokeMethod(obj, objectType.BaseType, method, argValues, argTypes);
else
throw new MissingMethodException();
#endif
}
public static void InvokeStaticMethod(this Type objectType, string method, object[] argValues, Type[] argTypes)
{
#if !NETFX_CORE
BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.OptionalParamBinding;
MethodInfo methodInfo = null;
if (argValues != null)
methodInfo = objectType.GetMethod(method, bindingAttr, null, argTypes, null);
else
methodInfo = objectType.GetMethod(method, bindingAttr);
if (methodInfo != null)
methodInfo.Invoke(null, argValues);
else if (objectType.BaseType != null)
InvokeStaticMethod(objectType.BaseType, method, argValues, argTypes);
else
throw new MissingMethodException();
#endif
}
}