using System;
using System.Reflection;
using UnityEngine;
namespace IllusionPlugin.Utils
{
///
/// A utility class providing reflection helper methods.
///
public static class ReflectionUtil
{
///
/// Sets a (potentially) private field on the target object.
///
/// the object instance
/// the field to set
/// the value to set it to
public static void SetPrivateField(this object obj, string fieldName, object value)
{
var prop = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(obj, value);
}
///
/// Gets the value of a (potentially) private field.
///
/// the type of te field (result casted)
/// the object instance to pull from
/// the name of the field to read
/// the value of the field
public static T GetPrivateField(this object obj, string fieldName)
{
var prop = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
var value = prop.GetValue(obj);
return (T) value;
}
///
/// Sets a (potentially) private propert on the target object.
///
/// the target object instance
/// the name of the property
/// the value to set it to
public static void SetPrivateProperty(this object obj, string propertyName, object value)
{
var prop = obj.GetType()
.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(obj, value, null);
}
///
/// Invokes a (potentially) private method.
///
/// the object to call from
/// the method name
/// the method parameters
/// the return value
public static object InvokePrivateMethod(this object obj, string methodName, params object[] methodParams)
{
MethodInfo dynMethod = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
return dynMethod.Invoke(obj, methodParams);
}
///
/// Invokes a (potentially) private method.
///
/// the return type
/// the object to call from
/// the method name to call
/// the method's parameters
/// the return value
public static T InvokePrivateMethod(this object obj, string methodName, params object[] methodParams)
{
return (T)InvokePrivateMethod(obj, methodName, methodParams);
}
///
/// Copies a component of type originalType to a component of overridingType on the destination GameObject.
///
/// the original component
/// the original component's type
/// the new component's type
/// the destination GameObject
///
public static Component CopyComponent(Component original, Type originalType, Type overridingType, GameObject destination)
{
var copy = destination.AddComponent(overridingType);
Type type = originalType;
while (type != typeof(MonoBehaviour))
{
CopyForType(type, original, copy);
type = type.BaseType;
}
return copy;
}
private static void CopyForType(Type type, Component source, Component destination)
{
FieldInfo[] myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetField);
foreach (FieldInfo fi in myObjectFields)
{
fi.SetValue(destination, fi.GetValue(source));
}
}
}
}