예제 #1
0
        /// <summary>
        /// Convert a ErrorCodeType to an host-byte ordered unsigned short
        /// </summary>
        /// <param name="mClass">The ErrorCodeType to convert</param>
        /// <returns>
        /// The unsigned short (16bits) matching the ErrorCodeType
        /// Return max UInt16 value if the mClass parameter is ErrorCodeType.Unmanaged
        /// </returns>
        public static UInt16 ErrorCodeTypeToUInt16(ErrorCodeType mClass)
        {
            foreach (FieldInfo field in typeof(ErrorCodeType).GetFields())
            {
                if (field.Name == mClass.ToString())
                {
                    Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                    if (fieldAttributes.Length == 1)
                    {
                        StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                        return(stunValueAttribute.Value);
                    }
                }
            }
            return(0xFFFF);
        }
예제 #2
0
        /// <summary>
        /// Convert an host-byte ordered unsigned short to a ErrorCodeType
        /// </summary>
        /// <param name="mType">An unsigned short representing an ErrorCodeType</param>
        /// <returns>
        /// The ErrorCodeType matching the unsigned short (16bits)
        /// Returns ErrorCodeType.Unmanaged if the unsigned short doesn't match any ErrorCodeType StunValue's
        /// </returns>
        public static ErrorCodeType UInt16ToErrorCodeType(UInt16 mType)
        {
            foreach (FieldInfo field in typeof(ErrorCodeType).GetFields())
            {
                Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                if (fieldAttributes.Length == 1)
                {
                    StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                    if (stunValueAttribute != null &&
                        stunValueAttribute.Value == mType)
                    {
                        return((ErrorCodeType)Enum.Parse(typeof(ErrorCodeType), field.Name));
                    }
                }
            }
            return(ErrorCodeType.Unmanaged);
        }