using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace Cinemachine.Utility
{
/// An ad-hoc collection of helpers for reflection, used by Cinemachine
/// or its editor tools in various places
[DocumentationSorting(0, DocumentationSortingAttribute.Level.Undoc)]
public static class ReflectionHelpers
{
/// Copy the fields from one object to another
/// The source object to copy from
/// The destination object to copy to
/// The mask to filter the attributes.
/// Only those fields that get caught in the filter will be copied
public static void CopyFields(
Object src, Object dst,
BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
{
if (src != null && dst != null)
{
Type type = src.GetType();
FieldInfo[] fields = type.GetFields(bindingAttr);
for (int i = 0; i < fields.Length; ++i)
if (!fields[i].IsStatic)
fields[i].SetValue(dst, fields[i].GetValue(src));
}
}
#if UNITY_EDITOR
/// Search the assembly for all types that match a predicate
/// The assembly to search
/// The type to look for
/// A list of types found in the assembly that inherit from the predicate
public static IEnumerable GetTypesInAssembly(
Assembly assembly, Predicate predicate)
{
if (assembly == null)
return null;
Type[] types = new Type[0];
try
{
types = assembly.GetTypes();
}
catch (Exception)
{
// Can't load the types in this assembly
}
types = (from t in types
where t != null && predicate(t)
select t).ToArray();
return types;
}
/// Get a type from a name
/// The name of the type to search for
/// The type matching the name, or null if not found
public static Type GetTypeInAllLoadedAssemblies(string typeName)
{
foreach (Type type in GetTypesInAllLoadedAssemblies(t => t.Name == typeName))
return type;
return null;
}
/// Search all assemblies for all types that match a predicate
/// The type to look for
/// A list of types found in the assembly that inherit from the predicate
public static IEnumerable GetTypesInAllLoadedAssemblies(Predicate predicate)
{
Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
List foundTypes = new List(100);
foreach (Assembly assembly in assemblies)
{
foreach (Type foundType in GetTypesInAssembly(assembly, predicate))
foundTypes.Add(foundType);
}
return foundTypes;
}
/// call GetTypesInAssembly() for all assemblies that match a predicate
/// Which assemblies to search
/// What type to look for
public static IEnumerable GetTypesInLoadedAssemblies(
Predicate assemblyPredicate, Predicate predicate)
{
Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
assemblies = assemblies.Where((Assembly assembly)
=> { return assemblyPredicate(assembly); }).OrderBy((Assembly ass)
=> { return ass.FullName; }).ToArray();
List foundTypes = new List(100);
foreach (Assembly assembly in assemblies)
{
foreach (Type foundType in GetTypesInAssembly(assembly, predicate))
foundTypes.Add(foundType);
}
return foundTypes;
}
public static bool TypeIsDefined(string fullname)
{
return (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.FullName == fullname
select type).Count() > 0;
}
#endif
/// Cheater extension to access internal field of an object
/// The type of the field
/// The object to access
/// The string name of the field to access
/// The value of the field in the objects
public static T AccessInternalField(this Type type, object obj, string memberName)
{
if (string.IsNullOrEmpty(memberName) || (type == null))
return default(T);
BindingFlags bindingFlags = BindingFlags.NonPublic;
if (obj != null)
bindingFlags |= BindingFlags.Instance;
else
bindingFlags |= BindingFlags.Static;
FieldInfo field = type.GetField(memberName, bindingFlags);
if ((field != null) && (field.FieldType == typeof(T)))
return (T)field.GetValue(obj);
else
return default(T);
}
/// Get the object owner of a field. This method processes
/// the '.' separator to get from the object that owns the compound field
/// to the object that owns the leaf field
/// The name of the field, which may contain '.' separators
/// the owner of the compound field
public static object GetParentObject(string path, object obj)
{
var fields = path.Split('.');
if (fields.Length == 1)
return obj;
var info = obj.GetType().GetField(
fields[0], BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
obj = info.GetValue(obj);
return GetParentObject(string.Join(".", fields, 1, fields.Length - 1), obj);
}
/// Returns a string path from an expression - mostly used to retrieve serialized properties
/// without hardcoding the field path. Safer, and allows for proper refactoring.
public static string GetFieldPath(Expression> expr)
{
MemberExpression me;
switch (expr.Body.NodeType)
{
case ExpressionType.MemberAccess:
me = expr.Body as MemberExpression;
break;
default:
throw new InvalidOperationException();
}
var members = new List();
while (me != null)
{
members.Add(me.Member.Name);
me = me.Expression as MemberExpression;
}
var sb = new StringBuilder();
for (int i = members.Count - 1; i >= 0; i--)
{
sb.Append(members[i]);
if (i > 0) sb.Append('.');
}
return sb.ToString();
}
}
}