From 5e3c5c0012025ca09ccc9363cb20318519f20957 Mon Sep 17 00:00:00 2001 From: Raistlin Wolfe Date: Mon, 12 Sep 2022 18:36:45 -0600 Subject: [PATCH] Create TypeExtensions.cs Provided Implementation for IsCastableFrom --- Scripts/TypeExtensions.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Scripts/TypeExtensions.cs diff --git a/Scripts/TypeExtensions.cs b/Scripts/TypeExtensions.cs new file mode 100644 index 0000000..5f79664 --- /dev/null +++ b/Scripts/TypeExtensions.cs @@ -0,0 +1,15 @@ +using System.Linq; +using System.Reflection; + +public static class TypeExtensions +{ + public static bool IsCastableFrom(this Type to, Type from) + { + if ( to.IsAssignableFrom ( from ) ) + return true; + return from.GetMethods ( BindingFlags.Public | BindingFlags.Static ).Any ( m => + { + return m.ReturnType == to && ( m.Name == "op_Implicit" || m.Name == "op_Explicit" ); + } ); + } +}