zoukankan      html  css  js  c++  java
  • 位运算用例

    枚举    

        public enum PermissionTypes : int
            {
                None = 0,
                浏览 = 1,
                分类管理 = 2,
                文档管理 = 4,
                权限管理 = 8,

                全部权限 = 浏览 | 分类管理 | 文档管理 | 权限管理
            }

    是否包含

            public static bool Has(int source, int value)
            {
                try
                {
                    return ((source & value) ==value);
                }
                catch
                {
                    return false;
                }
            }

    绑定枚举

     Utility.ItemListBind<Ph.PermissionTypes>(cbl);

    取值 

           public static int GetValueFromCheckBox(CheckBoxList cbl)
            {
                int ret = 0;
                int count = cbl.Items.Count;
                if (count > 0)
                    for (int i = 0; i < count; i++)
                    {
                        if (cbl.Items[i].Selected)
                            ret = (ret | int.Parse(cbl.Items[i].Value));
                    }
                return ret;
            }

    公用类Utility:

        /// <summary>
        /// 指定控件初始值(位运算专用)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="CBTarget"></param>
        /// <param name="strValue"></param>
        public static void SelectItemByValue<T>(CheckBoxList CBTarget, string strValue) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type");

            if (!Sxmobi.Tools.IsInt(strValue)) return;

            int iCount = CBTarget.Items.Count;
            for (int i = 0; i < iCount; i++)
                if (Ph.Has(int.Parse(strValue),int.Parse(CBTarget.Items[i].Value)))
                {
                    CBTarget.Items[i].Selected = true;
                }
        }

        public static void ItemListBind<T>(CheckBoxList CBTarget) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type");

            //绑定数据
            foreach (int myCode in Enum.GetValues(typeof(T)))
            {
                string myName = Enum.GetName(typeof(T), myCode);
                string myValue = myCode.ToString();
                ListItem myLi = new ListItem(myName, myValue);
                CBTarget.Items.Add(myLi);
            }
        }

  • 相关阅读:
    实现分享功能(分享到qq空间,新浪微博)
    AXIOS构建请求处理全局loading状态&&AXIOS避免重复请求loading多次出现
    Vue.use() 方法
    判断浏览器版本
    判断当前环境是ios还是安卓
    如何理解react中的super() super(props)
    JavaScript 函数调用时带括号和不带括号的区别
    npm 安装时 --save --dev 和 --save 区别
    npm 全局安装和局部安装的区别
    module.exports 与 exports
  • 原文地址:https://www.cnblogs.com/dashi/p/4034710.html
Copyright © 2011-2022 走看看