Пример #1
0
        private static bool CanBeAssigned(Type to, ESType esFrom, TypesAssignabilityVerificationKind verificationKind)
        {
            if (verificationKind == TypesAssignabilityVerificationKind.None)
            {
                return(true);
            }
            var cliTo   = verificationKind == TypesAssignabilityVerificationKind.HighLevel ? ToCLIType(to) : ToLowLevelCLIType(to);
            var cliFrom = verificationKind == TypesAssignabilityVerificationKind.HighLevel ? ToCLIType(esFrom) : ToLowLevelCLIType(esFrom);

            var from = esFrom.ToType();

            switch (cliTo)
            {
            case CLIType.Int32:
                return(cliFrom == CLIType.Int32 || cliFrom == CLIType.NativeInt || cliFrom == CLIType.Zero);

            case CLIType.NativeInt:
                return(cliFrom == CLIType.Int32 || cliFrom == CLIType.NativeInt || cliFrom == CLIType.Zero);

            case CLIType.Int64:
                return(cliFrom == CLIType.Int64 || cliFrom == CLIType.Zero);

            case CLIType.Float:
                return(cliFrom == CLIType.Float || cliFrom == CLIType.Zero);

            case CLIType.Struct:
                return(ReflectionExtensions.Equal(to, from));

            case CLIType.Pointer:
                if (cliFrom == CLIType.Zero || ReflectionExtensions.Equal(to, from))
                {
                    return(true);
                }
                if (cliFrom != CLIType.Pointer)
                {
                    return(false);
                }
                to   = to.GetElementType();
                from = from.GetElementType();
                return(to.IsValueType && from.IsValueType);

            case CLIType.Object:
                if (cliFrom == CLIType.Zero || ReflectionExtensions.Equal(to, from))
                {
                    return(true);
                }
                if (cliFrom != CLIType.Object)
                {
                    return(false);
                }
                var simpleESFrom = esFrom as SimpleESType;
                if (simpleESFrom != null)
                {
                    return(ReflectionExtensions.IsAssignableFrom(to, from));
                }
                var complexESFrom = (ComplexESType)esFrom;
                return(ReflectionExtensions.IsAssignableFrom(to, complexESFrom.BaseType) || complexESFrom.Interfaces.Any(interfaCe => ReflectionExtensions.IsAssignableFrom(to, interfaCe)));

            case CLIType.Zero:
                return(true);

            default:
                throw new InvalidOperationException($"CLI type '{cliTo}' is not valid at this point");
            }
        }
Пример #2
0
 protected static bool CanBeAssigned(Type to, Type from, TypesAssignabilityVerificationKind verificationKind)
 {
     return(CanBeAssigned(to, new SimpleESType(from), verificationKind));
 }