C#的反射机制

用C#在做服务器开发时,在处理服务器与客户端的响应时用到了C#的反射(通过字符串调用对应的函数)。

具体代码如下:

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using Config;
using LitJson;

public class JsonControl
{
    private Dictionary<ActionCode, JsonBase> m_dictionary = new Dictionary<ActionCode, JsonBase>();
    private Dictionary<StructTypeCode, ActionCode> m_typeDic = new Dictionary<StructTypeCode, ActionCode>();
    private Dictionary<ActionCode, object> m_objs = new Dictionary<ActionCode, object>();

    public JsonControl()
    {
        Debug.Log("JsonControl");
        //PhoneMessageJson messageJson = new PhoneMessageJson();
        //m_dictionary.Add(ActionCode.PhoneMessageJson, messageJson);

        AddDictionary();
    }

    /// <summary>
    /// 根据配置文件自动添加对应的类型到m_typeDic字典中
    /// </summary>
    private void AddDictionary()
    {
        m_typeDic.Add(StructTypeCode.IndexMsg, ActionCode.IndexJsonMsg);
    }

    /// <summary>
    /// 返回获取到的数据类型
    /// </summary>
    /// <param name="_typeCode"></param>
    /// <returns></returns>
    public object GetValue(StructTypeCode _typeCode)
    {
        ActionCode actionCode;
        m_typeDic.TryGetValue(_typeCode, out actionCode);
        object obj = null;
        m_objs.TryGetValue(actionCode, out obj);
        if (obj == null) return null;

        return obj;
    }

    /// <summary>
    /// 返回获取到的数据类型层级下的属性的long类型的值,多层级下的属性的数据会获取失败
    /// </summary>
    /// <param name="_typeCode"></param>
    /// <param name="_attitudeName"></param>
    /// <returns></returns>
    public long GetLongValue(StructTypeCode _typeCode, string _attitudeName)
    {
        ActionCode actionCode;
        m_typeDic.TryGetValue(_typeCode, out actionCode);
        object obj = null;
        m_objs.TryGetValue(actionCode, out obj);
        if (obj == null) return 0;

        try
        {
            var fieldInfo = obj.GetType().GetField(_attitudeName);
            return (long)fieldInfo.GetValue(obj);
        }
        catch (Exception)
        {
            return 0;
        }
    }

    /// <summary>
    /// 返回获取到的数据类型层级下的属性的string类型的值,多层级下的属性的数据会获取失败
    /// </summary>
    /// <param name="_typeCode"></param>
    /// <param name="_attitudeName"></param>
    /// <returns></returns>
    public string GetStringValue(StructTypeCode _typeCode, string _attitudeName)
    {
        ActionCode actionCode;
        m_typeDic.TryGetValue(_typeCode, out actionCode);
        object obj = null;
        m_objs.TryGetValue(actionCode, out obj);
        if (obj == null) return null;

        try
        {
            var fieldInfo = obj.GetType().GetField(_attitudeName);
            return (string)fieldInfo.GetValue(obj);
        }
        catch (Exception)
        {
            return null;
        }
    }

    /// <summary>
    /// 返回Bool值类型,获取数据失败时默认返回false,多层级下的属性的数据会获取失败
    /// </summary>
    /// <param name="_typeCode"></param>
    /// <param name="_attitudeName"></param>
    /// <returns></returns>
    public bool GetBoolValue(StructTypeCode _typeCode, string _attitudeName)
    {
        ActionCode actionCode;
        m_typeDic.TryGetValue(_typeCode, out actionCode);
        object obj = null;
        m_objs.TryGetValue(actionCode, out obj);
        if (obj == null) return false;

        try
        {
            var fieldInfo = obj.GetType().GetField(_attitudeName);
            return (bool)fieldInfo.GetValue(obj);
        }
        catch (Exception)
        {
            return false;
        }
    }

    /// <summary>
    /// 创建Json文件,返回字符串
    /// </summary>
    public string Create(object _object)
    {
        return JsonMapper.ToJson(_object);
    }

    /// <summary>
    /// Json的解析
    /// </summary>
    public void Analytical(string _json, ActionCode _actionCode)
    {
        //object instance = (object)Activator.CreateInstance(t); //创建对象
        JsonBase jsonBase = null;
        bool isGet = m_dictionary.TryGetValue(_actionCode, out jsonBase);
        if (!isGet)  //如果没有找到对应的,则创建并添加到字典中
        {
            string actionCodeName = Enum.GetName(typeof(ActionCode), _actionCode);
            Type t = Type.GetType(actionCodeName);//获取字符串对应的类型
            try
            {
                JsonBase instance = (JsonBase)Activator.CreateInstance(t); //创建对象
                m_dictionary.Add(_actionCode, instance);
                m_dictionary.TryGetValue(_actionCode, out jsonBase);
                //Debug.Log("添加字段成功");
            }
            catch
            {
                Debug.LogError("请创建枚举类型" + _actionCode.ToString() + "对应名字的脚本!");
                return;
            }
        }

        MethodInfo methodInfo = jsonBase.GetType().GetMethod("Analytical"); //获取成员函数
        if (methodInfo == null)
        {
            Debug.LogError(_actionCode.ToString() + "脚本中没有Analytical成员函数");
            return;
        }
        object[] param = new object[] { _json }; //成员函数所需参数
        object obj = methodInfo.Invoke(jsonBase, param);  //调用该成员函数

        MethodInfo methodInfo1 = jsonBase.GetType().GetMethod("GetData"); //获取成员函数
        if (methodInfo1 == null)
        {
            Debug.LogError(_actionCode.ToString() + "脚本中没有GetData成员函数");
            return;
        }
        object[] param1 = new object[] { };
        object obj1 = methodInfo1.Invoke(jsonBase, param1);  //调用该成员函数
        m_objs.Add(_actionCode, obj1);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值