public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
            }

#if !FEATURE_CORECLR && !MONO
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage))
            {
                FrameworkEventSource.Log.BeginGetMethodFromHandle();
            }
#endif

#if MONO
            MethodBase m = GetMethodFromHandleInternalType(handle.Value, declaringType.Value);
            if (m == null)
            {
                throw new ArgumentException("The handle is invalid.");
            }
#else
            MethodBase m = RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo());
#endif

#if !FEATURE_CORECLR && !MONO
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage) && declaringType != null && m != null)
            {
                FrameworkEventSource.Log.EndGetMethodFromHandle(declaringType.GetRuntimeType().GetFullNameForEtw(), m.GetFullNameForEtw());
            }
#endif

            return(m);
        }
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
            }

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage))
            {
                FrameworkEventSource.Log.BeginGetFieldFromHandle();
            }
#endif

            FieldInfo f = RuntimeType.GetFieldInfo(declaringType.GetRuntimeType(), handle.GetRuntimeFieldInfo());

#if !FEATURE_CORECLR
            if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage) && declaringType != null && f != null)
            {
                FrameworkEventSource.Log.EndGetFieldFromHandle(declaringType.GetRuntimeType().GetFullNameForEtw(), f.GetFullNameForEtw());
            }
#endif

            return(f);
        }
Exemple #3
0
        internal object[] CheckArguments(object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
        {
            int num = (parameters != null) ? parameters.Length : 0;

            object[]        objArray         = new object[num];
            ParameterInfo[] parametersNoCopy = null;
            for (int i = 0; i < num; i++)
            {
                object            o      = parameters[i];
                RuntimeTypeHandle handle = sig.Arguments[i];
                if (o == Type.Missing)
                {
                    if (parametersNoCopy == null)
                    {
                        parametersNoCopy = this.GetParametersNoCopy();
                    }
                    if (parametersNoCopy[i].DefaultValue == DBNull.Value)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Arg_VarMissNull"), "parameters");
                    }
                    o = parametersNoCopy[i].DefaultValue;
                }
                if (handle.IsInstanceOfType(o))
                {
                    objArray[i] = o;
                }
                else
                {
                    objArray[i] = handle.GetRuntimeType().CheckValue(o, binder, culture, invokeAttr);
                }
            }
            return(objArray);
        }
Exemple #4
0
 internal MdFieldInfo(int tkField, FieldAttributes fieldAttributes, RuntimeTypeHandle declaringTypeHandle, RuntimeType.RuntimeTypeCache reflectedTypeCache, BindingFlags bindingFlags)
     : base(reflectedTypeCache, declaringTypeHandle.GetRuntimeType(), bindingFlags)
 {
     this.m_tkField         = tkField;
     this.m_name            = (string)null;
     this.m_fieldAttributes = fieldAttributes;
 }
Exemple #5
0
        internal static RuntimeMethodInfo AssignAssociates(int tkMethod, RuntimeTypeHandle declaredTypeHandle, RuntimeTypeHandle reflectedTypeHandle)
        {
            if (MetadataToken.IsNullToken(tkMethod))
            {
                return(null);
            }
            bool flag = !declaredTypeHandle.Equals(reflectedTypeHandle);
            RuntimeMethodHandle methodHandle = declaredTypeHandle.GetModuleHandle().ResolveMethodHandle(tkMethod, declaredTypeHandle.GetInstantiation(), new RuntimeTypeHandle[0]);
            MethodAttributes    attributes   = methodHandle.GetAttributes();
            bool flag2 = (attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
            bool flag3 = (attributes & MethodAttributes.Virtual) != MethodAttributes.PrivateScope;

            if (flag)
            {
                if (flag2)
                {
                    return(null);
                }
                if (flag3 && ((declaredTypeHandle.GetAttributes() & TypeAttributes.ClassSemanticsMask) == TypeAttributes.AnsiClass))
                {
                    int slot = methodHandle.GetSlot();
                    methodHandle = reflectedTypeHandle.GetMethodAt(slot);
                }
            }
            MethodAttributes  attributes2 = attributes & MethodAttributes.MemberAccessMask;
            RuntimeMethodInfo methodBase  = RuntimeType.GetMethodBase(reflectedTypeHandle, methodHandle) as RuntimeMethodInfo;

            if (methodBase == null)
            {
                methodBase = reflectedTypeHandle.GetRuntimeType().Module.ResolveMethod(tkMethod, null, null) as RuntimeMethodInfo;
            }
            return(methodBase);
        }
Exemple #6
0
 public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
 {
     if (handle.IsNullHandle())
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
     }
     return(RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo()));
 }
Exemple #7
0
        public static MethodBase?GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(SR.Argument_InvalidHandle);
            }

            return(RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo()));
        }
Exemple #8
0
        public static FieldInfo GetFieldFromHandle(RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(SR.Argument_InvalidHandle);
            }

            return(RuntimeType.GetFieldInfo(declaringType.GetRuntimeType(), handle.GetRuntimeFieldInfo()));
        }
Exemple #9
0
 internal RuntimeMethodInfo(RuntimeMethodHandle handle, RuntimeTypeHandle declaringTypeHandle, RuntimeType.RuntimeTypeCache reflectedTypeCache, MethodAttributes methodAttributes, System.Reflection.BindingFlags bindingFlags)
 {
     this.m_toString           = null;
     this.m_bindingFlags       = bindingFlags;
     this.m_handle             = handle;
     this.m_reflectedTypeCache = reflectedTypeCache;
     this.m_parameters         = null;
     this.m_methodAttributes   = methodAttributes;
     this.m_declaringType      = declaringTypeHandle.GetRuntimeType();
 }
Exemple #10
0
        public static void RunClassConstructor(RuntimeTypeHandle type)
        {
            RuntimeType rt = type.GetRuntimeType();

            if (rt is null)
            {
                throw new ArgumentException(SR.InvalidOperation_HandleIsNotInitialized, nameof(type));
            }

            RunClassConstructor(new QCallTypeHandle(ref rt));
        }
Exemple #11
0
        public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
        {
            if (handle.IsNullHandle())
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle"));
            }
#if MONO
            MethodBase m = GetMethodFromHandleInternalType(handle.Value, declaringType.Value);
            if (m == null)
            {
                throw new ArgumentException("The handle is invalid.");
            }
            return(m);
#else
            return(RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo()));
#endif
        }
 private unsafe void PopulateEvents(RuntimeType.RuntimeTypeCache.Filter filter, RuntimeTypeHandle declaringTypeHandle, MetadataImport scope, int *tkAssociates, int cAssociates, Hashtable csEventInfos, List <RuntimeEventInfo> list)
 {
     //Require tkAssociates.AllocatedUntil >= cAssociates
     for (int i = 0; i < cAssociates; i++)
     {
         int        mdToken = tkAssociates[i];
         Utf8String name    = scope.GetName(mdToken);
         if (filter.Match(name))
         {
             bool             flag;
             RuntimeEventInfo item = new RuntimeEventInfo(mdToken, declaringTypeHandle.GetRuntimeType(), this.m_runtimeTypeCache, out flag);
             if ((declaringTypeHandle.Equals(this.m_runtimeTypeCache.RuntimeTypeHandle) || !flag) && (csEventInfos[item.Name] == null))
             {
                 csEventInfos[item.Name] = item;
                 list.Add(item);
             }
         }
     }
 }
        public static unsafe object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            int            num2;
            long           num4;
            CorElementType end         = CorElementType.End;
            long           num         = 0L;
            string         str         = scope.GetDefaultValue(token, out num, out num2, out end);
            RuntimeType    runtimeType = fieldTypeHandle.GetRuntimeType();

            if (!runtimeType.IsEnum || raw)
            {
                if (runtimeType == typeof(DateTime))
                {
                    num4 = 0L;
                    switch (end)
                    {
                    case CorElementType.I8:
                        num4 = num;
                        goto Label_0129;

                    case CorElementType.U8:
                        num4 = num;
                        goto Label_0129;

                    case CorElementType.Void:
                        return(DBNull.Value);
                    }
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                }
                switch (end)
                {
                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Boolean:
                    return(*(((int *)&num)) != 0);

                case CorElementType.Char:
                    return((char)*(((ushort *)&num)));

                case CorElementType.I1:
                    return(*(((sbyte *)&num)));

                case CorElementType.U1:
                    return(*(((byte *)&num)));

                case CorElementType.I2:
                    return(*(((short *)&num)));

                case CorElementType.U2:
                    return(*(((ushort *)&num)));

                case CorElementType.I4:
                    return(*(((int *)&num)));

                case CorElementType.U4:
                    return(*(((uint *)&num)));

                case CorElementType.I8:
                    return(num);

                case CorElementType.U8:
                    return((ulong)num);

                case CorElementType.R4:
                    return(*(((float *)&num)));

                case CorElementType.R8:
                    return(*(((double *)&num)));

                case CorElementType.String:
                    if (str == null)
                    {
                        return(string.Empty);
                    }
                    return(str);

                case CorElementType.Class:
                    return(null);
                }
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
            long num3 = 0L;

            switch (end)
            {
            case CorElementType.Void:
                return(DBNull.Value);

            case CorElementType.Char:
                num3 = *((ushort *)&num);
                break;

            case CorElementType.I1:
                num3 = *((sbyte *)&num);
                break;

            case CorElementType.U1:
                num3 = *((byte *)&num);
                break;

            case CorElementType.I2:
                num3 = *((short *)&num);
                break;

            case CorElementType.U2:
                num3 = *((ushort *)&num);
                break;

            case CorElementType.I4:
                num3 = *((int *)&num);
                break;

            case CorElementType.U4:
                num3 = *((uint *)&num);
                break;

            case CorElementType.I8:
                num3 = num;
                break;

            case CorElementType.U8:
                num3 = num;
                break;

            default:
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
            return(RuntimeType.CreateEnum(runtimeType, num3));

Label_0129:
            return(new DateTime(num4));
        }
Exemple #14
0
        [System.Security.SecurityCritical]  // auto-generated
        public static unsafe Object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = 0;
            long           buffer         = 0;
            int            length;
            String         stringVal;

            stringVal = scope.GetDefaultValue(token, out buffer, out length, out corElementType);

            RuntimeType fieldType = fieldTypeHandle.GetRuntimeType();

            if (fieldType.IsEnum && raw == false)
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    defaultValue = *(char *)&buffer;
                    break;

                case CorElementType.I1:
                    defaultValue = *(sbyte *)&buffer;
                    break;

                case CorElementType.U1:
                    defaultValue = *(byte *)&buffer;
                    break;

                case CorElementType.I2:
                    defaultValue = *(short *)&buffer;
                    break;

                case CorElementType.U2:
                    defaultValue = *(ushort *)&buffer;
                    break;

                case CorElementType.I4:
                    defaultValue = *(int *)&buffer;
                    break;

                case CorElementType.U4:
                    defaultValue = *(uint *)&buffer;
                    break;

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }

                return(RuntimeType.CreateEnum(fieldType, defaultValue));
            }
            else if (fieldType == typeof(DateTime))
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }

                return(new DateTime(defaultValue));
            }
            else
            {
                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    return(*(char *)&buffer);

                case CorElementType.I1:
                    return(*(sbyte *)&buffer);

                case CorElementType.U1:
                    return(*(byte *)&buffer);

                case CorElementType.I2:
                    return(*(short *)&buffer);

                case CorElementType.U2:
                    return(*(ushort *)&buffer);

                case CorElementType.I4:
                    return(*(int *)&buffer);

                case CorElementType.U4:
                    return(*(uint *)&buffer);

                case CorElementType.I8:
                    return(buffer);

                case CorElementType.U8:
                    return((ulong)buffer);

                case CorElementType.Boolean:
                    // The boolean value returned from the metadata engine is stored as a
                    // BOOL, which actually maps to an int. We need to read it out as an int
                    // to avoid problems on big-endian machines.
                    return(*(int *)&buffer != 0);

                case CorElementType.R4:
                    return(*(float *)&buffer);

                case CorElementType.R8:
                    return(*(double *)&buffer);

                case CorElementType.String:
                    // A string constant can be empty but never null.
                    // A nullref constant can only be type CorElementType.Class.
                    return(stringVal == null ? String.Empty : stringVal);

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    #endregion
                }
            }
        }
Exemple #15
0
        public unsafe static object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = CorElementType.End;
            long           num            = 0L;
            int            num2;
            string         defaultValue = scope.GetDefaultValue(token, out num, out num2, out corElementType);
            RuntimeType    runtimeType  = fieldTypeHandle.GetRuntimeType();

            if (runtimeType.IsEnum && !raw)
            {
                long value;
                switch (corElementType)
                {
                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    value = (long)((ulong)(*(ushort *)(&num)));
                    goto IL_C8;

                case CorElementType.I1:
                    value = (long)(*(sbyte *)(&num));
                    goto IL_C8;

                case CorElementType.U1:
                    value = (long)((ulong)(*(byte *)(&num)));
                    goto IL_C8;

                case CorElementType.I2:
                    value = (long)(*(short *)(&num));
                    goto IL_C8;

                case CorElementType.U2:
                    value = (long)((ulong)(*(ushort *)(&num)));
                    goto IL_C8;

                case CorElementType.I4:
                    value = (long)(*(int *)(&num));
                    goto IL_C8;

                case CorElementType.U4:
                    value = (long)((ulong)(*(uint *)(&num)));
                    goto IL_C8;

                case CorElementType.I8:
                    value = num;
                    goto IL_C8;

                case CorElementType.U8:
                    value = num;
                    goto IL_C8;
                }
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
IL_C8:
                return(RuntimeType.CreateEnum(runtimeType, value));
            }
            if (!(runtimeType == typeof(DateTime)))
            {
                switch (corElementType)
                {
                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Boolean:
                    return(*(int *)(&num) != 0);

                case CorElementType.Char:
                    return((char)(*(ushort *)(&num)));

                case CorElementType.I1:
                    return(*(sbyte *)(&num));

                case CorElementType.U1:
                    return(*(byte *)(&num));

                case CorElementType.I2:
                    return(*(short *)(&num));

                case CorElementType.U2:
                    return(*(ushort *)(&num));

                case CorElementType.I4:
                    return(*(int *)(&num));

                case CorElementType.U4:
                    return(*(uint *)(&num));

                case CorElementType.I8:
                    return(num);

                case CorElementType.U8:
                    return((ulong)num);

                case CorElementType.R4:
                    return(*(float *)(&num));

                case CorElementType.R8:
                    return(*(double *)(&num));

                case CorElementType.String:
                    if (defaultValue != null)
                    {
                        return(defaultValue);
                    }
                    return(string.Empty);

                case CorElementType.Class:
                    return(null);
                }
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
            if (corElementType != CorElementType.Void)
            {
                long ticks;
                if (corElementType != CorElementType.I8)
                {
                    if (corElementType != CorElementType.U8)
                    {
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    }
                    ticks = num;
                }
                else
                {
                    ticks = num;
                }
                return(new DateTime(ticks));
            }
            return(DBNull.Value);
        }
Exemple #16
0
        internal static unsafe void AssignAssociates(AssociateRecord *associates, int cAssociates, RuntimeTypeHandle declaringTypeHandle, RuntimeTypeHandle reflectedTypeHandle, out RuntimeMethodInfo addOn, out RuntimeMethodInfo removeOn, out RuntimeMethodInfo fireOn, out RuntimeMethodInfo getter, out RuntimeMethodInfo setter, out MethodInfo[] other, out bool composedOfAllPrivateMethods, out BindingFlags bindingFlags)
        {
            RuntimeMethodInfo info2;
            RuntimeMethodInfo info3;
            RuntimeMethodInfo info4;

            setter = (RuntimeMethodInfo)(info2 = null);
            getter = info3 = info2;
            fireOn = info4 = info3;
            addOn  = removeOn = info4;
            other  = null;
            Attributes attributes = Attributes.ComposedOfNoStaticMembers | Attributes.ComposedOfNoPublicMembers | Attributes.ComposedOfAllPrivateMethods | Attributes.ComposedOfAllVirtualMethods;

            while (reflectedTypeHandle.IsGenericVariable())
            {
                reflectedTypeHandle = reflectedTypeHandle.GetRuntimeType().BaseType.GetTypeHandleInternal();
            }
            bool      isInherited = !declaringTypeHandle.Equals(reflectedTypeHandle);
            ArrayList list        = new ArrayList();

            for (int i = 0; i < cAssociates; i++)
            {
                RuntimeMethodInfo info = AssignAssociates(associates[i].MethodDefToken, declaringTypeHandle, reflectedTypeHandle);
                if (info != null)
                {
                    MethodAttributes attributes2 = info.Attributes;
                    bool             flag2       = (attributes2 & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
                    bool             flag3       = (attributes2 & MethodAttributes.Virtual) != MethodAttributes.PrivateScope;
                    MethodAttributes attributes3 = attributes2 & MethodAttributes.MemberAccessMask;
                    bool             flag4       = attributes3 == MethodAttributes.Public;
                    bool             flag5       = (attributes2 & MethodAttributes.Static) != MethodAttributes.PrivateScope;
                    if (flag4)
                    {
                        attributes &= ~Attributes.ComposedOfNoPublicMembers;
                        attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                    }
                    else if (!flag2)
                    {
                        attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                    }
                    if (flag5)
                    {
                        attributes &= ~Attributes.ComposedOfNoStaticMembers;
                    }
                    if (!flag3)
                    {
                        attributes &= ~Attributes.ComposedOfAllVirtualMethods;
                    }
                    if (associates[i].Semantics == MethodSemanticsAttributes.Setter)
                    {
                        setter = info;
                    }
                    else if (associates[i].Semantics == MethodSemanticsAttributes.Getter)
                    {
                        getter = info;
                    }
                    else if (associates[i].Semantics == MethodSemanticsAttributes.Fire)
                    {
                        fireOn = info;
                    }
                    else if (associates[i].Semantics == MethodSemanticsAttributes.AddOn)
                    {
                        addOn = info;
                    }
                    else if (associates[i].Semantics == MethodSemanticsAttributes.RemoveOn)
                    {
                        removeOn = info;
                    }
                    else
                    {
                        list.Add(info);
                    }
                }
            }
            bool isPublic = (attributes & Attributes.ComposedOfNoPublicMembers) == 0;
            bool isStatic = (attributes & Attributes.ComposedOfNoStaticMembers) == 0;

            bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
            composedOfAllPrivateMethods = (attributes & Attributes.ComposedOfAllPrivateMethods) != 0;
            other = (MethodInfo[])list.ToArray(typeof(MethodInfo));
        }
Exemple #17
0
        public static unsafe object?GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = 0;
            long           buffer         = 0;
            int            length;
            string?        stringVal = scope.GetDefaultValue(token, out buffer, out length, out corElementType);

            RuntimeType fieldType = fieldTypeHandle.GetRuntimeType();

            if (fieldType.IsEnum && !raw)
            {
                // NOTE: Unlike in `TypeBuilder.SetConstantValue`, if `fieldType` describes
                // a nullable enum type `Nullable<TEnum>`, we do not unpack it to `TEnum` to
                // successfully enter this `if` clause. Default values of `TEnum?`-typed
                // parameters have been reported as values of the underlying type, changing
                // this now might be a breaking change.

                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.ELEMENT_TYPE_VOID:
                    return(DBNull.Value);

                case CorElementType.ELEMENT_TYPE_CHAR:
                    defaultValue = *(char *)&buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_I1:
                    defaultValue = *(sbyte *)&buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_U1:
                    defaultValue = *(byte *)&buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_I2:
                    defaultValue = *(short *)&buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_U2:
                    defaultValue = *(ushort *)&buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_I4:
                    defaultValue = *(int *)&buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_U4:
                    defaultValue = *(uint *)&buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_U8:
                    defaultValue = buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_CLASS:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }

                return(RuntimeType.CreateEnum(fieldType, defaultValue));
            }
            else if (fieldType == typeof(DateTime))
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.ELEMENT_TYPE_VOID:
                    return(DBNull.Value);

                case CorElementType.ELEMENT_TYPE_I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_U8:
                    defaultValue = buffer;
                    break;

                case CorElementType.ELEMENT_TYPE_CLASS:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }

                return(new DateTime(defaultValue));
            }
            else
            {
                return(corElementType switch
                {
                    CorElementType.ELEMENT_TYPE_VOID => DBNull.Value,
                    CorElementType.ELEMENT_TYPE_CHAR => *(char *)&buffer,
                    CorElementType.ELEMENT_TYPE_I1 => *(sbyte *)&buffer,
                    CorElementType.ELEMENT_TYPE_U1 => *(byte *)&buffer,
                    CorElementType.ELEMENT_TYPE_I2 => *(short *)&buffer,
                    CorElementType.ELEMENT_TYPE_U2 => *(ushort *)&buffer,
                    CorElementType.ELEMENT_TYPE_I4 => *(int *)&buffer,
                    CorElementType.ELEMENT_TYPE_U4 => *(uint *)&buffer,
                    CorElementType.ELEMENT_TYPE_I8 => buffer,
                    CorElementType.ELEMENT_TYPE_U8 => (ulong)buffer,
                    CorElementType.ELEMENT_TYPE_BOOLEAN => (*(int *)&buffer != 0),
                    CorElementType.ELEMENT_TYPE_R4 => *(float *)&buffer,
                    CorElementType.ELEMENT_TYPE_R8 => *(double *)&buffer,
                    CorElementType.ELEMENT_TYPE_STRING => stringVal ?? string.Empty,
                    CorElementType.ELEMENT_TYPE_CLASS => null,
                    _ => throw new FormatException(SR.Arg_BadLiteralFormat),
                });
Exemple #18
0
        public static unsafe object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = CorElementType.End;
            long           num1           = 0;
            int            length;
            string         defaultValue = scope.GetDefaultValue(token, out num1, out length, out corElementType);
            RuntimeType    runtimeType  = fieldTypeHandle.GetRuntimeType();

            if (runtimeType.IsEnum && !raw)
            {
                long num2;
                switch (corElementType)
                {
                case CorElementType.Void:
                    return((object)DBNull.Value);

                case CorElementType.Char:
                    num2 = (long)*(ushort *)&num1;
                    break;

                case CorElementType.I1:
                    num2 = (long)*(sbyte *)&num1;
                    break;

                case CorElementType.U1:
                    num2 = (long)*(byte *)&num1;
                    break;

                case CorElementType.I2:
                    num2 = (long)*(short *)&num1;
                    break;

                case CorElementType.U2:
                    num2 = (long)*(ushort *)&num1;
                    break;

                case CorElementType.I4:
                    num2 = (long)*(int *)&num1;
                    break;

                case CorElementType.U4:
                    num2 = (long)*(uint *)&num1;
                    break;

                case CorElementType.I8:
                    num2 = num1;
                    break;

                case CorElementType.U8:
                    num2 = num1;
                    break;

                default:
                    throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                }
                return(RuntimeType.CreateEnum(runtimeType, num2));
            }
            if ((Type)runtimeType == typeof(DateTime))
            {
                if (corElementType == CorElementType.Void)
                {
                    return((object)DBNull.Value);
                }
                long ticks;
                if (corElementType != CorElementType.I8)
                {
                    if (corElementType != CorElementType.U8)
                    {
                        throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
                    }
                    ticks = num1;
                }
                else
                {
                    ticks = num1;
                }
                return((object)new DateTime(ticks));
            }
            switch (corElementType)
            {
            case CorElementType.Void:
                return((object)DBNull.Value);

            case CorElementType.Boolean:
                return((object)((uint)*(int *)&num1 > 0U));

            case CorElementType.Char:
                return((object)(char)*(ushort *)&num1);

            case CorElementType.I1:
                return((object)*(sbyte *)&num1);

            case CorElementType.U1:
                return((object)*(byte *)&num1);

            case CorElementType.I2:
                return((object)*(short *)&num1);

            case CorElementType.U2:
                return((object)*(ushort *)&num1);

            case CorElementType.I4:
                return((object)*(int *)&num1);

            case CorElementType.U4:
                return((object)*(uint *)&num1);

            case CorElementType.I8:
                return((object)num1);

            case CorElementType.U8:
                return((object)(ulong)num1);

            case CorElementType.R4:
                return((object)*(float *)&num1);

            case CorElementType.R8:
                return((object)*(double *)&num1);

            case CorElementType.String:
                return((object)defaultValue ?? (object)string.Empty);

            case CorElementType.Class:
                return((object)null);

            default:
                throw new FormatException(Environment.GetResourceString("Arg_BadLiteralFormat"));
            }
        }
 public static void RunClassConstructor(RuntimeTypeHandle type)
 {
     _RunClassConstructor(type.GetRuntimeType());
 }
Exemple #20
0
        public static unsafe object GetValue(MetadataImport scope, int token, RuntimeTypeHandle fieldTypeHandle, bool raw)
        {
            CorElementType corElementType = 0;
            long           buffer         = 0;
            int            length;
            string         stringVal;

            stringVal = scope.GetDefaultValue(token, out buffer, out length, out corElementType);

            RuntimeType fieldType = fieldTypeHandle.GetRuntimeType();

            if (fieldType.IsEnum && raw == false)
            {
                // NOTE: Unlike in `TypeBuilder.SetConstantValue`, if `fieldType` describes
                // a nullable enum type `Nullable<TEnum>`, we do not unpack it to `TEnum` to
                // successfully enter this `if` clause. Default values of `TEnum?`-typed
                // parameters have been reported as values of the underlying type, changing
                // this now might be a breaking change.

                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    defaultValue = *(char *)&buffer;
                    break;

                case CorElementType.I1:
                    defaultValue = *(sbyte *)&buffer;
                    break;

                case CorElementType.U1:
                    defaultValue = *(byte *)&buffer;
                    break;

                case CorElementType.I2:
                    defaultValue = *(short *)&buffer;
                    break;

                case CorElementType.U2:
                    defaultValue = *(ushort *)&buffer;
                    break;

                case CorElementType.I4:
                    defaultValue = *(int *)&buffer;
                    break;

                case CorElementType.U4:
                    defaultValue = *(uint *)&buffer;
                    break;

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }

                return(RuntimeType.CreateEnum(fieldType, defaultValue));
            }
            else if (fieldType == typeof(DateTime))
            {
                long defaultValue = 0;

                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.I8:
                    defaultValue = buffer;
                    break;

                case CorElementType.U8:
                    defaultValue = buffer;
                    break;

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }

                return(new DateTime(defaultValue));
            }
            else
            {
                switch (corElementType)
                {
                    #region Switch

                case CorElementType.Void:
                    return(DBNull.Value);

                case CorElementType.Char:
                    return(*(char *)&buffer);

                case CorElementType.I1:
                    return(*(sbyte *)&buffer);

                case CorElementType.U1:
                    return(*(byte *)&buffer);

                case CorElementType.I2:
                    return(*(short *)&buffer);

                case CorElementType.U2:
                    return(*(ushort *)&buffer);

                case CorElementType.I4:
                    return(*(int *)&buffer);

                case CorElementType.U4:
                    return(*(uint *)&buffer);

                case CorElementType.I8:
                    return(buffer);

                case CorElementType.U8:
                    return((ulong)buffer);

                case CorElementType.Boolean:
                    // The boolean value returned from the metadata engine is stored as a
                    // BOOL, which actually maps to an int. We need to read it out as an int
                    // to avoid problems on big-endian machines.
                    return(*(int *)&buffer != 0);

                case CorElementType.R4:
                    return(*(float *)&buffer);

                case CorElementType.R8:
                    return(*(double *)&buffer);

                case CorElementType.String:
                    // A string constant can be empty but never null.
                    // A nullref constant can only be type CorElementType.Class.
                    return(stringVal == null ? string.Empty : stringVal);

                case CorElementType.Class:
                    return(null);

                default:
                    throw new FormatException(SR.Arg_BadLiteralFormat);
                    #endregion
                }
            }
        }