System.RuntimeTypeHandle.CanCastTo C# (CSharp) Method

CanCastTo() private method

private CanCastTo ( IntPtr target ) : bool
target IntPtr
return bool
        private extern bool CanCastTo(IntPtr target);
        internal bool CanCastTo(RuntimeTypeHandle target)

Same methods

RuntimeTypeHandle::CanCastTo ( RuntimeTypeHandle target ) : bool

Usage Example

Example #1
0
        public override bool IsAssignableFrom([NotNullWhen(true)] Type?c)
        {
            if (c is null)
            {
                return(false);
            }

            if (ReferenceEquals(c, this))
            {
                return(true);
            }

            // For runtime type, let the VM decide.
            if (c.UnderlyingSystemType is RuntimeType fromType)
            {
                // both this and c (or their underlying system types) are runtime types
                return(RuntimeTypeHandle.CanCastTo(fromType, this));
            }

            // Special case for TypeBuilder to be backward-compatible.
            if (c is System.Reflection.Emit.TypeBuilder)
            {
                // If c is a subclass of this class, then c can be cast to this type.
                if (c.IsSubclassOf(this))
                {
                    return(true);
                }

                if (IsInterface)
                {
                    return(c.ImplementInterface(this));
                }
                else if (IsGenericParameter)
                {
                    Type[] constraints = GetGenericParameterConstraints();
                    for (int i = 0; i < constraints.Length; i++)
                    {
                        if (!constraints[i].IsAssignableFrom(c))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }

            // For anything else we return false.
            return(false);
        }