mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 01:06:01 +08:00
17 lines
567 B
C#
17 lines
567 B
C#
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
public static class TypeExtensions
|
|
{
|
|
/// <summary> Determines whether an instance of a specified type can be assigned to a variable of the current type. </summary>
|
|
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" );
|
|
} );
|
|
}
|
|
}
|