GetTypeFromHandle() public static method

public static GetTypeFromHandle ( RuntimeTypeHandle typeHandle ) : Type
typeHandle RuntimeTypeHandle
return Type
        private object ResolveArgumentValue(CustomAttributeArgument argument, Type type)
        {
            var typeCode = argument.ArgumentType.TypeCode;
            var valuePtr = argument.GetArgumentValue();

            // If its an enum type
            if (argument.ArgumentType.ParentType.Handle == EnumTypePtr.Handle)
            {
                typeCode = argument.ArgumentType.ElementType.TypeCode;
            }

            switch (typeCode)
            {
            // 1 byte
            case TypeCode.Boolean:
                return((bool)(Intrinsic.Load8(valuePtr) != 0));

            case TypeCode.U1:
                return((byte)Intrinsic.Load8(valuePtr));

            case TypeCode.I1:
                return((sbyte)Intrinsic.Load8(valuePtr));

            // 2 bytes
            case TypeCode.Char:
                return((char)Intrinsic.Load16(valuePtr));

            case TypeCode.U2:
                return((ushort)Intrinsic.Load16(valuePtr));

            case TypeCode.I2:
                return((short)Intrinsic.Load16(valuePtr));

            // 4 bytes
            case TypeCode.U4:
                return((uint)Intrinsic.Load32(valuePtr));

            case TypeCode.I4:
                return((int)Intrinsic.Load32(valuePtr));

            case TypeCode.R4:
                return(Intrinsic.LoadR4(valuePtr));

            // 8 bytes
            case TypeCode.U8:
                return((ulong)Intrinsic.Load64(valuePtr));

            case TypeCode.I8:
                return((long)Intrinsic.Load64(valuePtr));

            case TypeCode.R8:
                return(Intrinsic.LoadR8(valuePtr));

            // SZArray
            case TypeCode.SZArray:
                return(ResolveArrayValue(argument, type));

            // String
            case TypeCode.String:
                return((string)Intrinsic.GetObjectFromAddress(valuePtr));

            default:
                if (type.FullName == "System.Type")
                {
                    // Get the argument type
                    var argTypeHandle = new RuntimeTypeHandle(argument.ArgumentType.Ptr);

                    return(Type.GetTypeFromHandle(argTypeHandle));
                }
                throw new ArgumentException();
            }
        }
Example #2
0
        internal static object CheckArgument(object srcObject, EETypePtr dstEEType, CheckArgumentSemantics semantics, BinderBundle binderBundle, Func <Type> getExactTypeForCustomBinder = null)
        {
            if (srcObject == null)
            {
                // null -> default(T)
                if (dstEEType.IsPointer)
                {
                    return(default(IntPtr));
                }
                else if (dstEEType.IsValueType && !dstEEType.IsNullable)
                {
                    if (semantics == CheckArgumentSemantics.SetFieldDirect)
                    {
                        throw CreateChangeTypeException(CommonRuntimeTypes.Object.TypeHandle.ToEETypePtr(), dstEEType, semantics);
                    }
                    return(Runtime.RuntimeImports.RhNewObject(dstEEType));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                EETypePtr srcEEType = srcObject.EETypePtr;

                if (RuntimeImports.AreTypesAssignable(srcEEType, dstEEType))
                {
                    return(srcObject);
                }

                object    dstObject;
                Exception exception = ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(srcObject, srcEEType, dstEEType, semantics, out dstObject);
                if (exception == null)
                {
                    return(dstObject);
                }

                if (binderBundle == null)
                {
                    throw exception;
                }

                // Our normal coercion rules could not convert the passed in argument but we were supplied a custom binder. See if it can do it.
                Type exactDstType;
                if (getExactTypeForCustomBinder == null)
                {
                    // We were called by someone other than DynamicInvokeParamHelperCore(). Those callers pass the correct dstEEType.
                    exactDstType = Type.GetTypeFromHandle(new RuntimeTypeHandle(dstEEType));
                }
                else
                {
                    // We were called by DynamicInvokeParamHelperCore(). He passes a dstEEType that enums folded to int and possibly other adjustments. A custom binder
                    // is app code however and needs the exact type.
                    exactDstType = getExactTypeForCustomBinder();
                }

                srcObject = binderBundle.ChangeType(srcObject, exactDstType);

                // For compat with desktop, the result of the binder call gets processed through the default rules again.
                dstObject = CheckArgument(srcObject, dstEEType, semantics, binderBundle: null, getExactTypeForCustomBinder: null);
                return(dstObject);
            }
        }
Example #3
0
 public static Type GetTargetType(TypedReference value)
 {
     return(Type.GetTypeFromHandle(value.type));
 }
Example #4
0
 private static ArgumentException CreateChangeTypeArgumentException(EETypePtr srcEEType, EETypePtr dstEEType)
 {
     return(new ArgumentException(SR.Format(SR.Arg_ObjObjEx, Type.GetTypeFromHandle(new RuntimeTypeHandle(srcEEType)), Type.GetTypeFromHandle(new RuntimeTypeHandle(dstEEType)))));
 }
Example #5
0
 public static Type GetTargetType(TypedReference value) => Type.GetTypeFromHandle(value._typeHandle);
Example #6
0
        internal static object?CheckArgument(object?srcObject, EETypePtr dstEEType, CheckArgumentSemantics semantics, BinderBundle?binderBundle, ref ArgSetupState argSetupState)
        {
            // Methods with ByRefLike types in signatures should be filtered out by the compiler
            Debug.Assert(!dstEEType.IsByRefLike);

            if (srcObject == null)
            {
                // null -> default(T)
                if (dstEEType.IsPointer)
                {
                    return(default(IntPtr));
                }
                else if (dstEEType.IsValueType && !dstEEType.IsNullable)
                {
                    if (semantics == CheckArgumentSemantics.SetFieldDirect)
                    {
                        throw CreateChangeTypeException(typeof(object).TypeHandle.ToEETypePtr(), dstEEType, semantics);
                    }
                    return(Runtime.RuntimeImports.RhNewObject(dstEEType));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                EETypePtr srcEEType = srcObject.EETypePtr;

                if (RuntimeImports.AreTypesAssignable(srcEEType, dstEEType))
                {
                    return(srcObject);
                }

                if (dstEEType.IsInterface)
                {
                    if (srcObject is Runtime.InteropServices.IDynamicInterfaceCastable castable &&
                        castable.IsInterfaceImplemented(new RuntimeTypeHandle(dstEEType), throwIfNotImplemented: false))
                    {
                        return(srcObject);
                    }
                }

                object    dstObject;
                Exception exception = ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(srcObject, srcEEType, dstEEType, semantics, out dstObject);
                if (exception == null)
                {
                    return(dstObject);
                }

                if (binderBundle == null)
                {
                    throw exception;
                }

                // Our normal coercion rules could not convert the passed in argument but we were supplied a custom binder. See if it can do it.
                Type exactDstType;
                if (Unsafe.IsNullRef(ref argSetupState))
                {
                    // We were called by someone other than DynamicInvokeParamHelperCore(). Those callers pass the correct dstEEType.
                    exactDstType = Type.GetTypeFromHandle(new RuntimeTypeHandle(dstEEType)) !;
                }
                else
                {
                    // We were called by DynamicInvokeParamHelperCore(). He passes a dstEEType that enums folded to int and possibly other adjustments. A custom binder
                    // is app code however and needs the exact type.
                    exactDstType = GetExactTypeForCustomBinder(argSetupState);
                }

                srcObject = binderBundle.ChangeType(srcObject, exactDstType);

                // For compat with desktop, the result of the binder call gets processed through the default rules again.
                dstObject = CheckArgument(srcObject, dstEEType, semantics, binderBundle: null, ref Unsafe.NullRef <ArgSetupState>());
                return(dstObject);
            }
        }
        private object ResolveArgumentValue(MetadataCAArgumentStruct *argument, Type type)
        {
            TypeCode typeCode = (TypeCode)(argument->ArgumentType->Attributes >> 24);
            var      valuePtr = MetadataCAArgumentStruct.GetArgumentAddress(argument);

            // If its an enum type
            if (argument->ArgumentType->ParentType == EnumTypePtr)
            {
                typeCode = (TypeCode)(argument->ArgumentType->ElementType->Attributes >> 24);
            }

            switch (typeCode)
            {
            // 1 byte
            case TypeCode.Boolean:
                return(((bool *)valuePtr)[0]);

            case TypeCode.U1:
                return(((byte *)valuePtr)[0]);

            case TypeCode.I1:
                return(((sbyte *)valuePtr)[0]);

            // 2 bytes
            case TypeCode.Char:
                return(((char *)valuePtr)[0]);

            case TypeCode.U2:
                return(((ushort *)valuePtr)[0]);

            case TypeCode.I2:
                return(((short *)valuePtr)[0]);

            // 4 bytes
            case TypeCode.U4:
                return(((uint *)valuePtr)[0]);

            case TypeCode.I4:
                return(((int *)valuePtr)[0]);

            case TypeCode.R4:
                return(((float *)valuePtr)[0]);

            // 8 bytes
            case TypeCode.U8:
                return(((ulong *)valuePtr)[0]);

            case TypeCode.I8:
                return(((long *)valuePtr)[0]);

            case TypeCode.R8:
                return(((double *)valuePtr)[0]);

            // SZArray
            case TypeCode.SZArray:
                return(ResolveArrayValue(argument, type));

            // String
            case TypeCode.String:
                return(x86Runtime.InitializeMetadataString(valuePtr));

            default:
                if (type.FullName == "System.Type")
                {
                    // Get the argument type
                    RuntimeTypeHandle argTypeHandle = new RuntimeTypeHandle();
                    ((uint **)&argTypeHandle)[0] = (uint *)argument->ArgumentType;
                    return(Type.GetTypeFromHandle(argTypeHandle));
                }
                throw new ArgumentException();
            }

            //return null;
        }
Example #8
0
 public static Type GetTypeFromHandle(RuntimeTypeHandle handle)
 => Type.GetTypeFromHandle(handle);