C#反射 从一个文本文档读取属性名和对应值,复制给对象

步骤不多,如下:

1.     由GetType()得到类型,

2.     var   f=GetType().GetField(string)得到对应名称的字段;

3.     f.FieldType可以得到字段对应的类型

4.     f.SetValue(this,value)可以对当前对象的字段进行赋值

文本内容如下:

名称:excel_def.txt

cfgMax =16
m_PathPrefix =Excel
m_ExcelFileMonster =怪物表.xlsx
m_ExcelFileSkill =技能基础表.xlsx
m_ExcelFileModel =模型资源表.xlsx
m_ExcelFileNpc     =NPC表.xlsx
m_TxtFileNpcModel =NpcModelPath.txt
m_TxtFileMonsterModel =MonsterModelPath.txt
m_MonsterTypeCol   =11
m_MonsterSceneCol =3
m_MonsterModelCol =4
m_MonsterSkillColList =5,6,7,8,9,10
m_TimeColList =3,4,5
m_SkillTableAniCol =3
m_ModelResCol =4
m_ModelNameCol =2
m_NpcModelCol =2


类中属性声明如下:

  //用于反射 自动解析 赋值  存储四个Excel
    public string m_ExcelFileMonster = "";//怪物Excel表
    public string m_ExcelFileSkill = "";       //技能
    public string m_ExcelFileModel = "";   //模型
    public string m_ExcelFileNpc;              //Npc
    //存储导出的NPC Monster模型
    public string m_TxtFileNpcModel;
    public string m_TxtFileMonsterModel;
    //
    public int m_MonsterTypeCol;       //怪物类型 Excel列号都是从1开始 非0
    public int m_MonsterSceneCol;     //所属场景 列号 从1开始
    public int m_MonsterModelCol;    //模型id 列号 从1开始
    public List<int> m_MonsterSkillColList = new List<int>();  //技能列列表   
    public List<int> m_TimeColList = new List<int>();             //技能触发时间列表
    
    public int m_SkillTableAniCol = 3;         //技能表中 动作列号
    public int m_ModelResCol = 4;             //模型表中 路径列号
    public int m_ModelNameCol = 2;         //模型表中 名称列号     
    public int m_NpcModelCol ;                 //Npc表中 模型id列号

    public string m_PathPrefix = "";       //标识路径Excel
    public int cfgMax = 0;


读取文档并赋值的代码片段

StreamReader sr = new StreamReader(cfgPath, Encoding.Default);
        //Debug.Log("code: "+sr.CurrentEncoding);


        string[] tmp = null;
        while (!sr.EndOfStream)
        {
            string line = sr.ReadLine();
            if (!string.IsNullOrEmpty(line))
            {
                tmp = line.Split('=');
                string part1 = tmp[0].Trim();
                string part2 = tmp[1].Trim();
            
                var f = GetType().GetField(part1);
                if (f == null)
                    continue;


                if (part1 != "cfgMax")
                    cfgCount++;


                if (f.FieldType == typeof(string))
                    f.SetValue(this, part2);
                else if (f.FieldType == typeof(int))
                    f.SetValue(this, int.Parse(part2));
                else if (f.FieldType == typeof (List<int>))
                {
                    string[] tmpArray = part2.Split(',');
                    List<int> arrayList = new List<int>();
                    int count = tmpArray.Length;
                    for (int i = 0; i < count; i++)
                    {
                        arrayList.Add(int.Parse(tmpArray[i]));                       
                    }    
                    f.SetValue(this, arrayList);
                }                
            }         
        }
        sr.Close();


总结:

反射给了我们更加灵活的处理方式。

在例子中,我们可以动态的读取配置文件,把它的内容分门别类的赋值给我们用到的属性。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值