【术】c#字典Dictionary自定义类作为key键

最近事情有点多,总是想直接贴代码,先放上去再说吧。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using LumenWorks.Framework.IO.Csv;

public class SkillGrowConfig
{
    public class SkillGrowConfigKey
    {
        public int id;
        public int level;

        public SkillGrowConfigKey()
        { }

        public SkillGrowConfigKey(int id, int level)
        {
            this.id = id;
            this.level = level;
        }

        public class EqualityComparer : IEqualityComparer<SkillGrowConfigKey>
        {
            public int GetHashCode(SkillGrowConfigKey key)
            {
                return key.id ^ key.level;
            }

            public bool Equals(SkillGrowConfigKey v1, SkillGrowConfigKey v2)
            {
                return v1.id == v2.id && v1.level == v2.level;
            }
        }
    }

    public class SkillGrowConfigValue
    {
        public int attack;
    }

    private Dictionary<SkillGrowConfigKey, SkillGrowConfigValue> m_SkillGrowConfigDic = new Dictionary<SkillGrowConfigKey, SkillGrowConfigValue>(new SkillGrowConfigKey.EqualityComparer());

    private string mPath = "SharedConfig/tactical_backpack";

    public SkillGrowConfigValue GetSkillValueByKey(SkillGrowConfigKey key)
    {
        if (m_SkillGrowConfigDic != null && m_SkillGrowConfigDic.ContainsKey(key))
        {
            return m_SkillGrowConfigDic[key];
        }
        return null;
    }

    public void LoadSkillGrowConfigInfo()
    {
        CsvReader reader = CsvReader.LoadTable(mPath + "/backpack_skill");
        reader.ReadNextRecord();
        while (reader.ReadNextRecord())
        {
            SkillGrowConfigKey skillKey = new SkillGrowConfigKey();
            int.TryParse(reader["skill_id"], out skillKey.id);
            int.TryParse(reader["level"], out skillKey.level);

            SkillGrowConfigValue skillValue = new SkillGrowConfigValue();
            int.TryParse(reader["attack"], out skillValue.attack);
            if (!m_SkillGrowConfigDic.ContainsKey(skillKey))
            {
                m_SkillGrowConfigDic.Add(skillKey, skillValue);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值