2020-08-18 17:38:09 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-09-08 17:07:10 +10:00
|
|
|
|
using System.Reflection;
|
2020-08-18 17:38:09 +10:00
|
|
|
|
|
|
|
|
|
namespace Explorer
|
|
|
|
|
{
|
|
|
|
|
public static class ReflectionExtensions
|
|
|
|
|
{
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#if CPP
|
2020-09-08 17:07:10 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extension to allow for easy, non-generic Il2Cpp casting.
|
|
|
|
|
/// The extension is on System.Object, but only Il2Cpp objects would be a valid target.
|
|
|
|
|
/// </summary>
|
2020-08-18 17:38:09 +10:00
|
|
|
|
public static object Il2CppCast(this object obj, Type castTo)
|
|
|
|
|
{
|
|
|
|
|
return ReflectionHelpers.Il2CppCast(obj, castTo);
|
|
|
|
|
}
|
2020-09-27 22:04:23 +10:00
|
|
|
|
#endif
|
2020-09-08 17:07:10 +10:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extension to safely try to get all Types from an Assembly, with a fallback for ReflectionTypeLoadException.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IEnumerable<Type> TryGetTypes(this Assembly asm)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return asm.GetTypes();
|
|
|
|
|
}
|
|
|
|
|
catch (ReflectionTypeLoadException e)
|
|
|
|
|
{
|
|
|
|
|
return e.Types.Where(t => t != null);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Empty<Type>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-18 17:38:09 +10:00
|
|
|
|
}
|
|
|
|
|
}
|