GetHashCode() public method

public GetHashCode ( ) : int
return int
Example #1
0
 /// <summary>
 /// 位域枚举是否包含指定的值,true:包含。
 /// .net中可以直接使用HasFlag判定
 /// </summary>
 public static bool Has(this Enum value, Enum target)
 {
     Guard.ArgumentNotNull(value, "Enum value");
     Guard.ArgumentNotNull(target, "Enum target");
     if (value.GetType() != target.GetType())
     {
         return(false);
     }
     return((value.GetHashCode() & target.GetHashCode()) == target.GetHashCode());
 }
Example #2
0
        /// <summary>
        /// 获取当前Enum的描述(DescriptionAttribute)
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
        /// <returns>枚举的Description</returns>
        public static string GetDescription(this Enum value, Boolean nameInstead = true)
        {
            Type type = value.GetType();
            Dictionary <int, string> cache = _descriptionCache.Get(type, () =>
            {
                Dictionary <int, string> values = new Dictionary <int, string>();
                foreach (Enum item in Enum.GetValues(type))
                {
                    string name     = Enum.GetName(type, item);
                    FieldInfo field = type.GetField(name);
                    DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attribute == null)
                    {
                        if (!nameInstead)
                        {
                            name = null;
                        }
                    }
                    else
                    {
                        name = attribute.Description;
                    }
                    values.Add(item.GetHashCode(), name);
                }
                return(values);
            });
            string description;

            lock (_descriptionLock)
            {
                cache.TryGetValue(value.GetHashCode(), out description);
            }
            return(description);
        }
Example #3
0
 /// <summary>
 /// 获取枚举值包含的位域值
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public static IEnumerable <Enum> GetFlagEnums(this Enum e)
 {
     if (e.GetHashCode() == 0)
     {
         return(new Enum[0]);
     }
     return(e.GetFieldValues().Where(item => e.IsFlagContains(item)));
 }
Example #4
0
        /// <summary>
        /// 除去位域枚举指定的一个枚举,返回处理后的枚举值
        /// </summary>
        public static T Remove <T>(this Enum value, Enum target)
        {
            Guard.ArgumentNotNull(value, "Enum value");
            Guard.ArgumentNotNull(target, "Enum target");

            var a = value.GetHashCode() & (~target.GetHashCode());

            return(a.ToEnum <T>());
        }
Example #5
0
    // CHANGE STATE
    public void ChangeState(System.Enum state)
    {
        if (_state == state)
        {
            return;
        }

        _state = state;
        _animator.SetInteger("State", _state.GetHashCode());
    }
Example #6
0
        /// <summary>
        /// Flag 형식의 Enum 에서 특정 Enum값이 <paramref name="flag"/> enum값을 가지는지 파악합니다.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static bool EnumHasFlag(this Enum value, Enum flag)
        {
            if(value.GetType() == flag.GetType())
            {
                var flagHashCode = flag.GetHashCode();
                return (value.GetHashCode() & flagHashCode) == flagHashCode;
            }

            return false;
        }
Example #7
0
 /// <summary>
 /// 枚举转Int数值
 /// </summary>
 /// <typeparam name="Enum">枚举对象类型</typeparam>
 /// <param name="enumObj">枚举对象</param>
 /// <returns></returns>
 public static int ToInt(this Enum enumObj)
 {
     try
     {
         return(enumObj.GetHashCode());
     }
     catch (Exception ex)
     {
         COMMON.Logs.Log.WriteException("EnumEx.EnumToInt异常", ex);
         throw ex;
     }
 }
Example #8
0
 static int GetHashCode(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         System.Enum obj = (System.Enum)ToLua.CheckObject <System.Enum>(L, 1);
         int         o   = obj.GetHashCode();
         LuaDLL.lua_pushinteger(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
        static StackObject *GetHashCode_21(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 1);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.Enum instance_of_this_method = (System.Enum) typeof(System.Enum).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = instance_of_this_method.GetHashCode();

            __ret->ObjectType = ObjectTypes.Integer;
            __ret->Value      = result_of_this_method;
            return(__ret + 1);
        }
Example #10
0
    static int GetHashCode(IntPtr L)
    {
        ToLua.CheckArgsCount(L, 1);
        System.Enum obj = (System.Enum)ToLua.CheckObject(L, 1, typeof(System.Enum));
        int         o;

        try
        {
            o = obj.GetHashCode();
        }
        catch (Exception e)
        {
            return(LuaDLL.luaL_error(L, e.Message));
        }

        LuaDLL.lua_pushinteger(L, o);
        return(1);
    }
Example #11
0
 /// <summary>
 /// 是否包含目标值
 /// </summary>
 /// <param name="e"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public static bool IsFlagContains(this Enum e, Enum value)
 {
     return(0 != (value.GetHashCode() & e.GetHashCode()));
 }
Example #12
0
 public static string ToValue(this System.Enum obj)
 {
     return(obj.GetHashCode().ToString());
 }
Example #13
0
 /// <summary>
 /// 返回枚举对应的参数
 /// </summary>
 /// <param name="roleType"></param>
 /// <returns></returns>
 public virtual ParameterDomain GetPrarmeterDomain(Enum enumType)
 {
     return GetParameterDomain(enumType.GetType().Name, enumType.GetHashCode().ToString());
 }
Example #14
0
 public static int Value(this Enum enums)
 {
     return(enums.GetHashCode());
 }
Example #15
0
 /// <summary>
 /// x是否包含其中一個記錄
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <returns></returns>
 public static bool AnyIsRight(Enum x, Enum[] y)
 {
     if (!y.HasRecord()) return false;
     return y.Any(y1 => (x.GetHashCode() & y1.GetHashCode()) == y1.GetHashCode());
 }
Example #16
0
 public static bool Declare(Enum source, Enum flag)
 {
     return Declare(source.GetHashCode(), flag.GetHashCode());
 }
Example #17
0
 /// <summary>
 /// 获取枚举值包含的位域值
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public static IEnumerable <Enum> GetFlagEnums(this Enum e)
 {
     return(e.GetHashCode() == 0 ? (new Enum[0]) : e.GetValues().Where(item => e.HasFlag(item)));
 }
Example #18
0
 public static int GetValue(this System.Enum e)
 {
     return(e.GetHashCode());
 }
Example #19
0
 public static string ToIntegerString(this System.Enum value)
 {
     return(value.GetHashCode().ToString());
 }
Example #20
0
 public static byte ToByte(this System.Enum value)
 {
     return((byte)value.GetHashCode());
 }
Example #21
0
 public static int ToInteger(this System.Enum value)
 {
     return(value.GetHashCode());
 }
Example #22
0
 static public string NextEnum_String <T>(this System.Enum eEnum)
     where T : struct
 {
     return(System.Enum.GetName(typeof(T), eEnum.GetHashCode() + 1));
 }
Example #23
0
 public FlagsResult IsDeclare(Enum flag)
 {
     return this.IsDeclare(flag.GetHashCode());
 }
Example #24
0
 public FlagsResult OrNotDeclare(Enum flag)
 {
     return this.GetResult(x => x || NotDeclare(this.Source, flag.GetHashCode()));
 }
Example #25
0
 public StringPair(string strKey, System.Enum eEnum)
 {
     this.strKey = strKey; this.strValue = eEnum.GetHashCode().ToString();
 }
        public static string Value(this System.Enum @enum)
        {
            var value = GetAttributeValue <EnumValueAttribute>(@enum);

            return(String.IsNullOrEmpty(value) ? @enum.GetHashCode().ToString() : value);
        }
Example #27
0
 public Flags(Enum source)
 {
     this.source = source.GetHashCode();
 }
Example #28
0
 /// <summary>
 /// 枚举转化int
 /// </summary>
 /// <param name="e"></param>
 /// <returns></returns>
 public static int ToInt(this System.Enum e)
 {
     return(e.GetHashCode());
 }
Example #29
0
 public static void WriteD(this BinaryWriter bw, Enum val)
 {
     bw.Write(val.GetHashCode());
 }
Example #30
0
 public static void Equals(Enum e, object obj, bool expected, bool hashExpected)
 {
     Assert.Equal(expected, e.Equals(obj));
     Assert.Equal(e.GetHashCode(), e.GetHashCode());
     if (obj != null)
     {
         Assert.Equal(hashExpected, e.GetHashCode().Equals(obj.GetHashCode()));
     }
 }
Example #31
0
        void Add(HappySourceLocation loc, Enum message, params object[] args)
        {
            string messageName = message.ToString();

            string msgfmt = Resources.CompileErrorMessages.ResourceManager.GetString(messageName);
            DebugAssert.IsNotNull(msgfmt, "Error message \"{0}\" not found in resources", messageName.ToString());
            _sourceErrors.Add(loc.Unit, Util.Format(msgfmt, args), loc.Span, message.GetHashCode(), Severity.FatalError);
        }