コード例 #1
0
ファイル: MarshalHelpers.cs プロジェクト: wilfriedb/runtime
        internal static MarshallerKind GetMarshallerKind(
            TypeDesc type,
            int?parameterIndex,
            EmbeddedSignatureData[] customModifierData,
            MarshalAsDescriptor marshalAs,
            bool isReturn,
            bool isAnsi,
            MarshallerType marshallerType,
            out MarshallerKind elementMarshallerKind)
        {
            elementMarshallerKind = MarshallerKind.Invalid;

            bool isByRef = false;

            if (type.IsByRef)
            {
                isByRef = true;

                type = type.GetParameterType();

                if (!type.IsPrimitive && type.IsValueType && marshallerType != MarshallerType.Field &&
                    HasCopyConstructorCustomModifier(parameterIndex, customModifierData))
                {
                    return(MarshallerKind.BlittableValueClassWithCopyCtor);
                }

                // Compat note: CLR allows ref returning blittable structs for IJW
                if (isReturn)
                {
                    return(MarshallerKind.Invalid);
                }
            }
            TypeSystemContext context    = type.Context;
            NativeTypeKind    nativeType = NativeTypeKind.Default;
            bool isField = marshallerType == MarshallerType.Field;

            if (marshalAs != null)
            {
                nativeType = marshalAs.Type;
            }

            //
            // Determine MarshalerKind
            //
            if (type.IsPrimitive)
            {
                switch (type.Category)
                {
                case TypeFlags.Void:
                    return(MarshallerKind.VoidReturn);

                case TypeFlags.Boolean:
                    switch (nativeType)
                    {
                    case NativeTypeKind.Default:
                    case NativeTypeKind.Boolean:
                        return(MarshallerKind.Bool);

                    case NativeTypeKind.U1:
                    case NativeTypeKind.I1:
                        return(MarshallerKind.CBool);

                    case NativeTypeKind.VariantBool:
                        if (context.Target.IsWindows)
                        {
                            return(MarshallerKind.VariantBool);
                        }
                        else
                        {
                            return(MarshallerKind.Invalid);
                        }

                    default:
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Char:
                    switch (nativeType)
                    {
                    case NativeTypeKind.I1:
                    case NativeTypeKind.U1:
                        return(MarshallerKind.AnsiChar);

                    case NativeTypeKind.I2:
                    case NativeTypeKind.U2:
                        return(MarshallerKind.UnicodeChar);

                    case NativeTypeKind.Default:
                        if (isAnsi)
                        {
                            return(MarshallerKind.AnsiChar);
                        }
                        else
                        {
                            return(MarshallerKind.UnicodeChar);
                        }

                    default:
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.SByte:
                case TypeFlags.Byte:
                    if (nativeType == NativeTypeKind.I1 || nativeType == NativeTypeKind.U1 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Int16:
                case TypeFlags.UInt16:
                    if (nativeType == NativeTypeKind.I2 || nativeType == NativeTypeKind.U2 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Int32:
                case TypeFlags.UInt32:
                    if (nativeType == NativeTypeKind.I4 || nativeType == NativeTypeKind.U4 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Int64:
                case TypeFlags.UInt64:
                    if (nativeType == NativeTypeKind.I8 || nativeType == NativeTypeKind.U8 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.IntPtr:
                case TypeFlags.UIntPtr:
                    if (nativeType == NativeTypeKind.SysInt || nativeType == NativeTypeKind.SysUInt || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Single:
                    if (nativeType == NativeTypeKind.R4 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Double:
                    if (nativeType == NativeTypeKind.R8 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsValueType)
            {
                if (type.IsEnum)
                {
                    return(MarshallerKind.Enum);
                }

                if (InteropTypes.IsSystemDateTime(context, type))
                {
                    if (nativeType == NativeTypeKind.Default ||
                        nativeType == NativeTypeKind.Struct)
                    {
                        return(MarshallerKind.OleDateTime);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsHandleRef(context, type))
                {
                    if (nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.HandleRef);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsSystemDecimal(context, type))
                {
                    if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.Decimal);
                    }
                    else if (nativeType == NativeTypeKind.LPStruct && !isField)
                    {
                        return(MarshallerKind.BlittableStructPtr);
                    }
                    else if (nativeType == NativeTypeKind.Currency)
                    {
                        return(MarshallerKind.OleCurrency);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsSystemGuid(context, type))
                {
                    if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableStruct);
                    }
                    else if (nativeType == NativeTypeKind.LPStruct && !isField)
                    {
                        return(MarshallerKind.BlittableStructPtr);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsSystemArgIterator(context, type))
                {
                    // Don't want to fall through to the blittable/haslayout case
                    return(MarshallerKind.Invalid);
                }

                bool isBlittable = MarshalUtils.IsBlittableType(type);

                // Blittable generics are allowed to be marshalled with the following exceptions:
                // * ByReference<T>: This represents an interior pointer and is not actually blittable
                // * Nullable<T>: We don't want to be locked into the default behavior as we may want special handling later
                // * Vector64<T>: Represents the __m64 ABI primitive which requires currently unimplemented handling
                // * Vector128<T>: Represents the __m128 ABI primitive which requires currently unimplemented handling
                // * Vector256<T>: Represents the __m256 ABI primitive which requires currently unimplemented handling
                // * Vector<T>: Has a variable size (either __m128 or __m256) and isn't readily usable for interop scenarios
                // We can't block these types for field scenarios for back-compat reasons.

                if (type.HasInstantiation && !isField && (!isBlittable ||
                                                          InteropTypes.IsSystemByReference(context, type) ||
                                                          InteropTypes.IsSystemSpan(context, type) ||
                                                          InteropTypes.IsSystemReadOnlySpan(context, type) ||
                                                          InteropTypes.IsSystemNullable(context, type) ||
                                                          InteropTypes.IsSystemRuntimeIntrinsicsVector64T(context, type) ||
                                                          InteropTypes.IsSystemRuntimeIntrinsicsVector128T(context, type) ||
                                                          InteropTypes.IsSystemRuntimeIntrinsicsVector256T(context, type) ||
                                                          InteropTypes.IsSystemNumericsVectorT(context, type)))
                {
                    // Generic types cannot be marshaled.
                    return(MarshallerKind.Invalid);
                }

                if (isBlittable)
                {
                    if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    return(MarshallerKind.BlittableStruct);
                }
                else if (((MetadataType)type).HasLayout())
                {
                    if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    return(MarshallerKind.Struct);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsSzArray)
            {
#if READYTORUN
                // We don't want the additional test/maintenance cost of this in R2R.
                if (isByRef)
                {
                    return(MarshallerKind.Invalid);
                }
#else
                _ = isByRef;
#endif

                if (nativeType == NativeTypeKind.Default)
                {
                    nativeType = NativeTypeKind.Array;
                }

                switch (nativeType)
                {
                case NativeTypeKind.Array:
                {
                    if (isField)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    var arrayType = (ArrayType)type;

                    elementMarshallerKind = GetArrayElementMarshallerKind(
                        arrayType,
                        marshalAs,
                        isAnsi);

                    // If element is invalid type, the array itself is invalid
                    if (elementMarshallerKind == MarshallerKind.Invalid)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    if (elementMarshallerKind == MarshallerKind.AnsiChar)
                    {
                        return(MarshallerKind.AnsiCharArray);
                    }
                    else if (elementMarshallerKind == MarshallerKind.UnicodeChar ||         // Arrays of unicode char should be marshalled as blittable arrays
                             elementMarshallerKind == MarshallerKind.Enum ||
                             elementMarshallerKind == MarshallerKind.BlittableValue)
                    {
                        return(MarshallerKind.BlittableArray);
                    }
                    else
                    {
                        return(MarshallerKind.Array);
                    }
                }

                case NativeTypeKind.ByValArray:             // fix sized array
                {
                    var arrayType = (ArrayType)type;
                    elementMarshallerKind = GetArrayElementMarshallerKind(
                        arrayType,
                        marshalAs,
                        isAnsi);

                    // If element is invalid type, the array itself is invalid
                    if (elementMarshallerKind == MarshallerKind.Invalid)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    if (elementMarshallerKind == MarshallerKind.AnsiChar)
                    {
                        return(MarshallerKind.ByValAnsiCharArray);
                    }
                    else
                    {
                        return(MarshallerKind.ByValArray);
                    }
                }

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsPointer)
            {
                if (nativeType == NativeTypeKind.Default)
                {
                    var pointedAtType = type.GetParameterType();
                    if (!pointedAtType.IsPrimitive && !type.IsEnum && marshallerType != MarshallerType.Field &&
                        HasCopyConstructorCustomModifier(parameterIndex, customModifierData))
                    {
                        return(MarshallerKind.BlittableValueClassWithCopyCtor);
                    }
                    return(MarshallerKind.BlittableValue);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsFunctionPointer)
            {
                if (nativeType == NativeTypeKind.Func || nativeType == NativeTypeKind.Default)
                {
                    return(MarshallerKind.BlittableValue);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsDelegate)
            {
                if (type.HasInstantiation)
                {
                    // Generic types cannot be marshaled.
                    return(MarshallerKind.Invalid);
                }

                if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Func)
                {
                    return(MarshallerKind.FunctionPointer);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsString)
            {
                switch (nativeType)
                {
                case NativeTypeKind.LPWStr:
                    return(MarshallerKind.UnicodeString);

                case NativeTypeKind.LPStr:
                    return(MarshallerKind.AnsiString);

                case NativeTypeKind.LPUTF8Str:
                    return(MarshallerKind.UTF8String);

                case NativeTypeKind.LPTStr:
                    return(MarshallerKind.UnicodeString);

                case NativeTypeKind.ByValTStr:
                    if (isAnsi)
                    {
                        elementMarshallerKind = MarshallerKind.AnsiChar;
                        return(MarshallerKind.ByValAnsiString);
                    }
                    else
                    {
                        elementMarshallerKind = MarshallerKind.UnicodeChar;
                        return(MarshallerKind.ByValUnicodeString);
                    }

                case NativeTypeKind.TBStr:
                case NativeTypeKind.BStr:
                    return(MarshallerKind.BSTRString);

                case NativeTypeKind.AnsiBStr:
                    return(MarshallerKind.AnsiBSTRString);

                case NativeTypeKind.Default:
                    if (isAnsi)
                    {
                        return(MarshallerKind.AnsiString);
                    }
                    else
                    {
                        return(MarshallerKind.UnicodeString);
                    }

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsObject)
            {
                if (nativeType == NativeTypeKind.AsAny)
                {
                    return(isAnsi ? MarshallerKind.AsAnyA : MarshallerKind.AsAnyW);
                }
                else
                if (context.Target.IsWindows)
                {
                    if ((isField && nativeType == NativeTypeKind.Default) ||
                        nativeType == NativeTypeKind.Intf ||
                        nativeType == NativeTypeKind.IUnknown)
                    {
                        return(MarshallerKind.ComInterface);
                    }
                    else
                    {
                        return(MarshallerKind.Variant);
                    }
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (InteropTypes.IsStringBuilder(context, type))
            {
                switch (nativeType)
                {
                case NativeTypeKind.Default:
                    if (isAnsi)
                    {
                        return(MarshallerKind.AnsiStringBuilder);
                    }
                    else
                    {
                        return(MarshallerKind.UnicodeStringBuilder);
                    }

                case NativeTypeKind.LPStr:
                    return(MarshallerKind.AnsiStringBuilder);

                case NativeTypeKind.LPWStr:
                    return(MarshallerKind.UnicodeStringBuilder);

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (InteropTypes.IsSafeHandle(context, type))
            {
                if (nativeType == NativeTypeKind.Default)
                {
                    return(MarshallerKind.SafeHandle);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (InteropTypes.IsCriticalHandle(context, type))
            {
                if (nativeType == NativeTypeKind.Default)
                {
                    return(MarshallerKind.CriticalHandle);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type is MetadataType mdType && mdType.HasLayout())
            {
                if (type.HasInstantiation)
                {
                    // Generic types cannot be marshaled.
                    return(MarshallerKind.Invalid);
                }

                if (!isField && nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.LPStruct)
                {
                    return(MarshallerKind.LayoutClassPtr);
                }
                else if (isField && (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct))
                {
                    return(MarshallerKind.LayoutClass);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
コード例 #2
0
        internal static MarshallerKind GetMarshallerKind(
            TypeDesc type,
            MarshalAsDescriptor marshalAs,
            bool isReturn,
            bool isAnsi,
            MarshallerType marshallerType,
            out MarshallerKind elementMarshallerKind)
        {
            elementMarshallerKind = MarshallerKind.Invalid;

            bool isByRef = false;

            if (type.IsByRef)
            {
                isByRef = true;

                type = type.GetParameterType();

                // Compat note: CLR allows ref returning blittable structs for IJW
                if (isReturn)
                {
                    return(MarshallerKind.Invalid);
                }
            }
            TypeSystemContext context    = type.Context;
            NativeTypeKind    nativeType = NativeTypeKind.Default;
            bool isField = marshallerType == MarshallerType.Field;

            if (marshalAs != null)
            {
                nativeType = marshalAs.Type;
            }

            //
            // Determine MarshalerKind
            //
            // This mostly resembles .NET Framework CLR and .NET Native code as we need to match their behavior
            //
            if (type.IsPrimitive)
            {
                switch (type.Category)
                {
                case TypeFlags.Void:
                    return(MarshallerKind.VoidReturn);

                case TypeFlags.Boolean:
                    switch (nativeType)
                    {
                    case NativeTypeKind.Default:
                    case NativeTypeKind.Boolean:
                        return(MarshallerKind.Bool);

                    case NativeTypeKind.U1:
                    case NativeTypeKind.I1:
                        return(MarshallerKind.CBool);

                    default:
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Char:
                    switch (nativeType)
                    {
                    case NativeTypeKind.I1:
                    case NativeTypeKind.U1:
                        return(MarshallerKind.AnsiChar);

                    case NativeTypeKind.I2:
                    case NativeTypeKind.U2:
                        return(MarshallerKind.UnicodeChar);

                    case NativeTypeKind.Default:
                        if (isAnsi)
                        {
                            return(MarshallerKind.AnsiChar);
                        }
                        else
                        {
                            return(MarshallerKind.UnicodeChar);
                        }

                    default:
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.SByte:
                case TypeFlags.Byte:
                    if (nativeType == NativeTypeKind.I1 || nativeType == NativeTypeKind.U1 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Int16:
                case TypeFlags.UInt16:
                    if (nativeType == NativeTypeKind.I2 || nativeType == NativeTypeKind.U2 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Int32:
                case TypeFlags.UInt32:
                    if (nativeType == NativeTypeKind.I4 || nativeType == NativeTypeKind.U4 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Int64:
                case TypeFlags.UInt64:
                    if (nativeType == NativeTypeKind.I8 || nativeType == NativeTypeKind.U8 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.IntPtr:
                case TypeFlags.UIntPtr:
                    if (nativeType == NativeTypeKind.SysInt || nativeType == NativeTypeKind.SysUInt || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Single:
                    if (nativeType == NativeTypeKind.R4 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                case TypeFlags.Double:
                    if (nativeType == NativeTypeKind.R8 || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableValue);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsValueType)
            {
                if (type.IsEnum)
                {
                    return(MarshallerKind.Enum);
                }

                if (InteropTypes.IsSystemDateTime(context, type))
                {
                    if (nativeType == NativeTypeKind.Default ||
                        nativeType == NativeTypeKind.Struct)
                    {
                        return(MarshallerKind.OleDateTime);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsHandleRef(context, type))
                {
                    if (nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.HandleRef);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsSystemDecimal(context, type))
                {
                    if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.Decimal);
                    }
                    else if (nativeType == NativeTypeKind.LPStruct && !isField && !isReturn)
                    {
                        return(MarshallerKind.BlittableStructPtr);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsSystemGuid(context, type))
                {
                    if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default)
                    {
                        return(MarshallerKind.BlittableStruct);
                    }
                    else if (nativeType == NativeTypeKind.LPStruct && !isField && !isReturn)
                    {
                        return(MarshallerKind.BlittableStructPtr);
                    }
                    else
                    {
                        return(MarshallerKind.Invalid);
                    }
                }
                else if (InteropTypes.IsSystemArgIterator(context, type))
                {
                    // Don't want to fall through to the blittable/haslayout case
                    return(MarshallerKind.Invalid);
                }

                bool isBlittable = MarshalUtils.IsBlittableType(type);

                // Blittable generics are allowed to be marshalled with the following exceptions:
                // * ByReference<T>: This represents an interior pointer and is not actually blittable
                // * Nullable<T>: We don't want to be locked into the default behavior as we may want special handling later
                // * Vector64<T>: Represents the __m64 ABI primitive which requires currently unimplemented handling
                // * Vector128<T>: Represents the __m128 ABI primitive which requires currently unimplemented handling
                // * Vector256<T>: Represents the __m256 ABI primitive which requires currently unimplemented handling
                // * Vector<T>: Has a variable size (either __m128 or __m256) and isn't readily usable for inteorp scenarios

                if (type.HasInstantiation && (!isBlittable ||
                                              InteropTypes.IsSystemByReference(context, type) ||
                                              InteropTypes.IsSystemNullable(context, type) ||
                                              InteropTypes.IsSystemRuntimeIntrinsicsVector64T(context, type) ||
                                              InteropTypes.IsSystemRuntimeIntrinsicsVector128T(context, type) ||
                                              InteropTypes.IsSystemRuntimeIntrinsicsVector256T(context, type) ||
                                              InteropTypes.IsSystemNumericsVectorT(context, type)))
                {
                    // Generic types cannot be marshaled.
                    return(MarshallerKind.Invalid);
                }

                if (isBlittable)
                {
                    if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    return(MarshallerKind.BlittableStruct);
                }
                else if (((MetadataType)type).HasLayout())
                {
                    if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    return(MarshallerKind.Struct);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsSzArray)
            {
#if READYTORUN
                // We don't want the additional test/maintenance cost of this in R2R.
                if (isByRef)
                {
                    return(MarshallerKind.Invalid);
                }
#else
                _ = isByRef;
#endif

                if (nativeType == NativeTypeKind.Default)
                {
                    nativeType = NativeTypeKind.Array;
                }

                switch (nativeType)
                {
                case NativeTypeKind.Array:
                {
                    if (isField || isReturn)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    var arrayType = (ArrayType)type;

                    elementMarshallerKind = GetArrayElementMarshallerKind(
                        arrayType,
                        marshalAs,
                        isAnsi);

                    // If element is invalid type, the array itself is invalid
                    if (elementMarshallerKind == MarshallerKind.Invalid)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    if (elementMarshallerKind == MarshallerKind.AnsiChar)
                    {
                        return(MarshallerKind.AnsiCharArray);
                    }
                    else if (elementMarshallerKind == MarshallerKind.UnicodeChar ||         // Arrays of unicode char should be marshalled as blittable arrays
                             elementMarshallerKind == MarshallerKind.Enum ||
                             elementMarshallerKind == MarshallerKind.BlittableValue)
                    {
                        return(MarshallerKind.BlittableArray);
                    }
                    else
                    {
                        return(MarshallerKind.Array);
                    }
                }

                case NativeTypeKind.ByValArray:             // fix sized array
                {
                    var arrayType = (ArrayType)type;
                    elementMarshallerKind = GetArrayElementMarshallerKind(
                        arrayType,
                        marshalAs,
                        isAnsi);

                    // If element is invalid type, the array itself is invalid
                    if (elementMarshallerKind == MarshallerKind.Invalid)
                    {
                        return(MarshallerKind.Invalid);
                    }

                    if (elementMarshallerKind == MarshallerKind.AnsiChar)
                    {
                        return(MarshallerKind.ByValAnsiCharArray);
                    }
                    else
                    {
                        return(MarshallerKind.ByValArray);
                    }
                }

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsPointer)
            {
#if READYTORUN
                TypeDesc parameterType = ((PointerType)type).ParameterType;

                if ((!parameterType.IsEnum &&
                     !parameterType.IsPrimitive &&
                     !MarshalUtils.IsBlittableType(parameterType)) ||
                    parameterType.IsGCPointer)
                {
                    // Pointers cannot reference marshaled structures.  Use ByRef instead.
                    return(MarshallerKind.Invalid);
                }
#else
                // Do not bother enforcing the above artificial restriction
                // See https://github.com/dotnet/coreclr/issues/27800
#endif

                if (nativeType == NativeTypeKind.Default)
                {
                    return(MarshallerKind.BlittableValue);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsFunctionPointer)
            {
                if (nativeType == NativeTypeKind.Func || nativeType == NativeTypeKind.Default)
                {
                    return(MarshallerKind.BlittableValue);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsDelegate)
            {
                if (type.HasInstantiation)
                {
                    // Generic types cannot be marshaled.
                    return(MarshallerKind.Invalid);
                }

                if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Func)
                {
                    return(MarshallerKind.FunctionPointer);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsString)
            {
                switch (nativeType)
                {
                case NativeTypeKind.LPWStr:
                    return(MarshallerKind.UnicodeString);

                case NativeTypeKind.LPStr:
                    return(MarshallerKind.AnsiString);

                case NativeTypeKind.LPUTF8Str:
                    return(MarshallerKind.UTF8String);

                case NativeTypeKind.LPTStr:
                    return(MarshallerKind.UnicodeString);

                case NativeTypeKind.ByValTStr:
                    if (isAnsi)
                    {
                        elementMarshallerKind = MarshallerKind.AnsiChar;
                        return(MarshallerKind.ByValAnsiString);
                    }
                    else
                    {
                        elementMarshallerKind = MarshallerKind.UnicodeChar;
                        return(MarshallerKind.ByValUnicodeString);
                    }

                case NativeTypeKind.Default:
                    if (isAnsi)
                    {
                        return(MarshallerKind.AnsiString);
                    }
                    else
                    {
                        return(MarshallerKind.UnicodeString);
                    }

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type.IsObject)
            {
                if (nativeType == NativeTypeKind.AsAny)
                {
                    return(isAnsi ? MarshallerKind.AsAnyA : MarshallerKind.AsAnyW);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (InteropTypes.IsStringBuilder(context, type))
            {
                switch (nativeType)
                {
                case NativeTypeKind.Default:
                    if (isAnsi)
                    {
                        return(MarshallerKind.AnsiStringBuilder);
                    }
                    else
                    {
                        return(MarshallerKind.UnicodeStringBuilder);
                    }

                case NativeTypeKind.LPStr:
                    return(MarshallerKind.AnsiStringBuilder);

                case NativeTypeKind.LPWStr:
                    return(MarshallerKind.UnicodeStringBuilder);

                default:
                    return(MarshallerKind.Invalid);
                }
            }
            else if (InteropTypes.IsSafeHandle(context, type))
            {
                if (nativeType == NativeTypeKind.Default)
                {
                    return(MarshallerKind.SafeHandle);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (InteropTypes.IsCriticalHandle(context, type))
            {
                if (nativeType == NativeTypeKind.Default)
                {
                    return(MarshallerKind.CriticalHandle);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }
            else if (type is MetadataType mdType && mdType.HasLayout())
            {
                if (type.HasInstantiation)
                {
                    // Generic types cannot be marshaled.
                    return(MarshallerKind.Invalid);
                }

                if (!isField && nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.LPStruct)
                {
                    return(MarshallerKind.LayoutClassPtr);
                }
                else if (isField && (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct))
                {
                    return(MarshallerKind.LayoutClass);
                }
                else
                {
                    return(MarshallerKind.Invalid);
                }
            }