1. 程式人生 > >【術】c#字典Dictionary自定義類作為key鍵

【術】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); } } } }