C#中的Dictionary.ContainsKey()没有在Dictionary中找到一个键

Jay*_*len 2 c# authentication permissions dictionary user-permissions

我正在尝试为特权/权限编写一个类.

我试图在C#中使用二进制基本权限系统.

我得到它,我相信它应该工作.但是,它没有按预期工作.

之后,我通过我的代码逐步完成,我可以在方法中看到一个问题,_mapPermissions该行if (dic.ContainsKey(vv))每次都返回false.

关键是一个类的实例具有2倍的值(即secionName,KeyIndex)

这是我的全班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;

namespace POS
{
    class Roles
    {

        private Dictionary<PermissionsKey, int> systemPermissions = new Dictionary<PermissionsKey, int>();
        private Dictionary<PermissionsKey, int> userPermissions = new Dictionary<PermissionsKey, int>();

        public Roles()
        {
            this._getSystemPrivleges();
            this._getUserPrivleges();

            this._display(systemPermissions);
            this._display(userPermissions);

        }

        public bool hasAccess(string sectionName, string[] keys)
        {

            if (    this.systemPermissions.Count == 0
                 || this.userPermissions.Count == 0
                 || keys.Count() == 0
                 || String.IsNullOrEmpty(sectionName) 
             )
            {
                return false;
            }

            int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
            int userPerm = this._mapPermissions(sectionName, keys, userPermissions);



            int check =  systemReq & userPerm;

            Common.Alert("System Value" + systemReq + Environment.NewLine +"User Value " + userPerm + Environment.NewLine + "AND Results " + check);


            if (check == 0)
            {
                return false;
            }

            return true;

        }

        private int _mapPermissions(string secName, string[] keys, Dictionary<PermissionsKey, int> dic)
        {

            int newKey = 0;
            var vv = new PermissionsKey();

            foreach (string k in keys)
            {
                vv.sectionName = secName;
                vv.KeyIndex = k;

                if (dic.ContainsKey(vv))
                {
                    newKey |= dic[vv] ;
                }
            }

            return newKey;
        }



        private void _getSystemPrivleges()
        {
            var db = new dbConnetion();

            string sql =   " SELECT SQL_CACHE "
                         + " KeyIndex, Value, sectionName "
                         + " FROM role_keys "
                         + " WHERE status = 'active' ";

            foreach (var i in db.getData(sql, null, r => new SystemPermissions()
                                                           {
                                                                 pIndex = r["KeyIndex"].ToString()
                                                               , pSec = r["sectionName"].ToString()
                                                               , pValue = r["Value"].ToString()
                                                           }
                                           )
                    )
            {

                var vv = new PermissionsKey();
                vv.sectionName = i.pSec;
                vv.KeyIndex = i.pIndex;
                systemPermissions.Add(vv, Convert.ToInt32(i.pValue));

            }
        }

        private void _getUserPrivleges()
        {
            var db = new dbConnetion();

            string sql = " SELECT SQL_CACHE k.sectionName, k.KeyIndex, k.value "
                        +" FROM users AS su "
                        +" INNER JOIN role_relation AS r ON r.roleID = su.roleID "
                        +" INNER JOIN role_keys AS k ON k.KeyID = r.KeyID "
                        +" WHERE su.status = 'active' AND su.userID = @userid ";

            var parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter("@userid", UserInfo.UserID));

            foreach (var i in db.getData(sql, parms , r => new SystemPermissions()
                                                            {
                                                                  pIndex = r["KeyIndex"].ToString()
                                                                , pSec = r["sectionName"].ToString()
                                                                , pValue = r["Value"].ToString()
                                                            }
                                           )
                    )
            {
                var vv = new PermissionsKey();
                vv.sectionName = i.pSec;
                vv.KeyIndex = i.pIndex;

                userPermissions.Add(vv, Convert.ToInt32(i.pValue));
            }
        }



        private void _display(Dictionary<PermissionsKey, int> dic)
        {
            string str = "";
            foreach (KeyValuePair<PermissionsKey, int> d in dic)
            {
                var vv = new PermissionsKey();
                var c = d.Key;

                str += c.sectionName + "_" + c.KeyIndex + "  =>  " + d.Value.ToString() + Environment.NewLine;

            }

            Common.Alert(str);
        }



        private int _BinToInt(string str)
        {
            Regex binary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
            int val = -1;

            if (binary.IsMatch(str))
            {
                val = Convert.ToInt32(str, 2);
            }

            return val;
        }

        private string _IntToBin(int number)
        {
            return Convert.ToString(number, 2);
        }


    }

    class SystemPermissions{
        public string pIndex;
        public string pSec;
        public string pValue;
    }

    class PermissionsKey
    {
        public string sectionName;
        public string KeyIndex;
    }
}
Run Code Online (Sandbox Code Playgroud)

我就这样使用这个类

    var role = new Roles();
    string[] aa = { "view", "use" };

    if (role.hasAccess("system", aa))
    {
        Common.Alert("Welcome");
    }
    else
    {
        Common.Alert("NOP!");
    }
Run Code Online (Sandbox Code Playgroud)

注意Common.Alert()它只是显示一个消息的方法(即MessageBox)

上面的代码在运行时显示以下内容

1)systemPermissions字典的内容按以下顺序PermissionsKey.sectionName_ PermissionsKey.KeyIndex=>值(ie. 1 , 2, 4, 8, 16, 32, 64 .....) system_do => 1 system_use => 2 system_view => 4 test_use => 1 test_view => 2

2)usePermissions字典的内容

system_do = > 1
test_use => 1
Run Code Online (Sandbox Code Playgroud)

问题是2个变量systemReq 并且userPerm每次都返回0.我不知道为什么

 int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
 int userPerm = this._mapPermissions(sectionName, keys, userPermissions);
Run Code Online (Sandbox Code Playgroud)

psu*_*003 9

为了使用某些东西作为字典键并期望它遵循你对所谓的平等的期望,你实际上需要定义什么被认为是相同的.

在您的PermissionsKey类中,您不会覆盖Equals,这意味着默认的Equals方法仍在使用中(这是对象引用,并且对于每个特定对象都是唯一的).

要修复,您需要实际告诉字典如何评估相等性.无论何时覆盖Equals,您还应该覆盖GetHashCode:

class PermissionsKey
{
    private string sectionName;
    private string keyIndex;

    public string SectionName { get { return sectionName; } }
    public string KeyIndex { get { return keyIndex; } }

    public PermissionsKey(string sectionName, string keyIndex)
    {
        this.sectionName = sectionName;
        this.keyIndex = keyIndex;
    }

    public override bool Equals(object obj)
    {
        var key = obj as PermissionsKey;

        if (key == null)
            return false;

        return sectionName.Equals(key.sectionName) &&
               keyIndex.Equals(key.keyIndex);
    }


    //Credit to Jon Skeet from https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
    //  for inspiration for this method
    public override int GetHashCode()
    {
        unchecked // Overflow is fine, just wrap
        {
            int hash = (int) 2166136261;
            // Suitable nullity checks etc, of course :)
            hash = hash * 16777619 ^ sectionName.GetHashCode();
            hash = hash * 16777619 ^ keyIndex.GetHashCode();
            return hash;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您正在重写Equals以实际定义相等比较.无论何时尝试比较2个对象(特别是在Dictionary对象中,但是通过调用它来执行任何其他时间.Equals),您应该定义自己的比较.如果你不这样做,那么你只是使用默认的相等比较,这是一个纯粹的异议比较和2个对象,除非从同一个确切的对象创建永远不会是等于.

同样,当您重写Equals时,强烈建议您也覆盖GetHashCode.HashCode定义了对象落入哪个"桶".遵循Equals实现的良好哈希码将通过仅将具有相同哈希码的对象放入相同的桶中来帮助加速比较.标准建议是2个对象,Equals将始终具有相同的哈希码,但是2个不等于的对象可能具有相同的哈希码.实际上,它是框架检查不平等的快速方法.如果你得到2个不同的哈希码,框架知道对象不相等,但如果哈希码相同,那么它就会检查相等性.

如果你不能或不想重写Equals和GetHashCode,那么你也可以选择IEqualityComparer<T>在字典的构造函数中定义一个,

您只需传递一个实现的对象,IEqualityComparer<T>并在该对象中定义您想要的相等比较.

此外,任何你打算使用作为字典键应该永远是可变的,特别是用于计算平等和计算哈希码的领域,否则你的钥匙可能会丢失为对象的散列码不匹配什么最初放入字典.我修改了您的原始示例,将PermissionsKey中的字段更改为不可变属性,并添加了一个构造函数,以允许您在最初创建时将它们设置一次.