Beispiel #1
0
        public void EnumTest()
        {
            AreEqual(
                BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static,
                EnumHelper.AsEnum <BindingFlags>(EnumHelper.AsUInt64(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)));

            AreEqual(
                TypeCode.Int32,
                EnumHelper.AsEnum <TypeCode>(EnumHelper.AsUInt64(TypeCode.Int32)));

            AreEqual(nameof(TypeCode.Int32), EnumHelper.GetEnumName(TypeCode.Int32));
            IsNull(EnumHelper.GetEnumName((TypeCode)999));
            AreEqual(nameof(BindingFlags.Public), EnumHelper.GetEnumName(BindingFlags.Public));
            IsNull(EnumHelper.GetEnumName(BindingFlags.Public | BindingFlags.Instance));

            var chars = stackalloc char[100];

            AreEqual(
                (BindingFlags)0x800000,
                EnumHelper.FormatEnumFlags(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | (BindingFlags)0x800000, chars, 100, out var charsWritten));

            AreEqual(
                $"{nameof(BindingFlags.Instance)}{EnumHelper.EnumSeperator}{nameof(BindingFlags.Static)}{EnumHelper.EnumSeperator}{nameof(BindingFlags.Public)}",
                StringHelper.ToString(chars, charsWritten));

            AreEqual(
                true,
                EnumHelper.TryParseEnum <BindingFlags>(new Ps <char>(chars, charsWritten), out var value));

            AreEqual(
                BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public,
                value);
        }
        T IValueReader.ReadEnum <T>()
        {
            if (ReadBoolean())
            {
                var values = (T[])typeof(T).GetEnumValues();

                return(values[random.Next(0, values.Length)]);
            }
            else if (ReadBoolean())
            {
                var values = (T[])typeof(T).GetEnumValues();

                ulong val = 0;

                for (int i = random.Next(0, values.Length); i >= 0; i--)
                {
                    val |= EnumHelper.AsUInt64(values[random.Next(0, values.Length)]);
                }

                return(EnumHelper.AsEnum <T>(val));
            }
            else
            {
                return(EnumHelper.AsEnum <T>(ReadUInt64()));
            }
        }