Example #1
0
        public unsafe int CompareTo(Object target)
        {
            if (target == null)
            {
                return(1);
            }

            if (target == this)
            {
                return(0);
            }

            if (this.EETypePtr != target.EETypePtr)
            {
                throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, target.GetType().ToString(), this.GetType().ToString()));
            }

            fixed(IntPtr *pThisObj = &this.m_pEEType, pTargetObj = &target.m_pEEType)
            {
                IntPtr pThisValue   = Object.GetAddrOfPinnedObjectFromEETypeField(pThisObj);
                IntPtr pTargetValue = Object.GetAddrOfPinnedObjectFromEETypeField(pTargetObj);

                switch (this.EETypePtr.CorElementType)
                {
                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1:
                    return((*(sbyte *)pThisValue == *(sbyte *)pTargetValue) ? 0 : (*(sbyte *)pThisValue < *(sbyte *)pTargetValue) ? -1 : 1);

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1:
                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN:
                    return((*(byte *)pThisValue == *(byte *)pTargetValue) ? 0 : (*(byte *)pThisValue < *(byte *)pTargetValue) ? -1 : 1);

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2:
                    return((*(short *)pThisValue == *(short *)pTargetValue) ? 0 : (*(short *)pThisValue < *(short *)pTargetValue) ? -1 : 1);

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2:
                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR:
                    return((*(ushort *)pThisValue == *(ushort *)pTargetValue) ? 0 : (*(ushort *)pThisValue < *(ushort *)pTargetValue) ? -1 : 1);

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4:
                    return((*(int *)pThisValue == *(int *)pTargetValue) ? 0 : (*(int *)pThisValue < *(int *)pTargetValue) ? -1 : 1);

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4:
                    return((*(uint *)pThisValue == *(uint *)pTargetValue) ? 0 : (*(uint *)pThisValue < *(uint *)pTargetValue) ? -1 : 1);

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8:
                    return((*(long *)pThisValue == *(long *)pTargetValue) ? 0 : (*(long *)pThisValue < *(long *)pTargetValue) ? -1 : 1);

                case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8:
                    return((*(ulong *)pThisValue == *(ulong *)pTargetValue) ? 0 : (*(ulong *)pThisValue < *(ulong *)pTargetValue) ? -1 : 1);

                default:
                    Environment.FailFast("Unexpected enum underlying type");
                    return(0);
                }
            }
        }
Example #2
0
        public override int GetHashCode()
        {
            unsafe
            {
                fixed(IntPtr *pObj = &this.m_pEEType)
                {
                    IntPtr pValue = Object.GetAddrOfPinnedObjectFromEETypeField(pObj);

                    switch (this.EETypePtr.CorElementType)
                    {
                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN:
                        return((*(bool *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR:
                        return((*(char *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1:
                        return((*(sbyte *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1:
                        return((*(byte *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2:
                        return((*(short *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2:
                        return((*(ushort *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4:
                        return((*(int *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4:
                        return((*(uint *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8:
                        return((*(long *)pValue).GetHashCode());

                    case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8:
                        return((*(ulong *)pValue).GetHashCode());

                    default:
                        Environment.FailFast("Unexpected enum underlying type");
                        return(0);
                    }
                }
            }
        }
Example #3
0
        public Boolean HasFlag(Enum flag)
        {
            if (flag == null)
            {
                throw new ArgumentNullException("flag");
            }
            Contract.EndContractBlock();

            if (!(this.EETypePtr == flag.EETypePtr))
            {
                throw new ArgumentException(SR.Format(SR.Argument_EnumTypeDoesNotMatch, flag.GetType(), this.GetType()));
            }

            unsafe
            {
                fixed(IntPtr *pThisObj = &this.m_pEEType, pFlagObj = &flag.m_pEEType)
                {
                    IntPtr pThisValue = Object.GetAddrOfPinnedObjectFromEETypeField(pThisObj);
                    IntPtr pFlagValue = Object.GetAddrOfPinnedObjectFromEETypeField(pFlagObj);

                    switch (this.EETypePtr.CorElementTypeInfo.Log2OfSize)
                    {
                    case 0:
                        return(((*(byte *)pThisValue) & (*(byte *)pFlagValue)) == *(byte *)pFlagValue);

                    case 1:
                        return(((*(ushort *)pThisValue) & (*(ushort *)pFlagValue)) == *(ushort *)pFlagValue);

                    case 2:
                        return(((*(uint *)pThisValue) & (*(uint *)pFlagValue)) == *(uint *)pFlagValue);

                    case 3:
                        return(((*(ulong *)pThisValue) & (*(ulong *)pFlagValue)) == *(ulong *)pFlagValue);

                    default:
                        Environment.FailFast("Unexpected enum underlying type");
                        return(false);
                    }
                }
            }
        }