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);}} }}

版权声明:本文为博主原创文章,未经博主允许不得转载。



分享给朋友:
您可能感兴趣的文章:
随机阅读: