• Unity数据加密☀️反射的用法:变量、属性、方法、重载,反射在DLL中的使用方法


    在这里插入图片描述**


    🟥 关于asmdef、DLL的特殊注意事项

    若你要反射的脚本所在的文件夹或上层节点中含有asmdef,此时调用反射代码会报如下错误:

    ArgumentException: Type cannot be null.


    你添加了Assembly Definition Reference,此时运行正常了。但如果你将C#文件打包成DLL,又会报上面的错误。

    DLL不支持直接直接使用 Type.GetType。



    因此不要尝试网上的教程了,直接按照本文的最终方案来吧。



    🟧GetType多平台方法

        private Type GetType(string typeName)
        {
            var type = Type.GetType(typeName);
            if (type != null) return type;
            foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
            {
                type = a.GetType(typeName);
                if (type != null)
                    return type;
            }
    
            return null;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13



    🟧反射属性

        private void SetProperty(string className, string propertyName, object value)
        {
    		//SKODE.Test:反射的类,如果有命名空间就需要加上。
            Type tarClass = GetType("SKODE.Test");
            Component tarComponent = gameObject.GetComponent(tarClass);
            PropertyInfo tarProperty =
                tarClass.GetProperty("hi", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    
            //将 tarComponent 组件的 tarProperty属性 设置为 true
            tarProperty.SetValue(tarComponent, true);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11



    🟨反射变量

        private void SetField()
        {
            //SKODE.Test:反射的类,如果有命名空间就需要加上。
            Type tarClass = GetType("SKODE.Test");
            Component tarComponent = gameObject.GetComponent(tarClass);
            FieldInfo tarField =
                tarClass.GetField("hii", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    
            //将 tarComponent 组件的 tarField变量 设置为 true
            tarField.SetValue(tarComponent, true);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11



    🟩反射方法

        private void CallMethod()
        {
            //SKODE.Test:反射的类,如果有命名空间就需要加上。
            Type tarClass = GetType("SKODE.Test");
            Component tarComponent = gameObject.GetComponent(tarClass);
            MethodInfo tarMethod =
                tarClass.GetMethod("Call", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    
            //调用 tarComponent 组件的 tarMethod方法,并传入参数 parameters。
            //若不需要传入参数,则传入null即可。
            object[] parameters = { true };
            tarMethod.Invoke(tarComponent, parameters);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13



    🟦 反射重载方法

    在反射重载方法时,如果调用此重载方法,会产生"发现不明确的匹配"的错误。

    AmbiguousMatchException: Ambiguous match found.



    解决方案如下:

    GetMethod("MethodName", new Type [] { typeof(参数类型)});
    
    • 1

    其中type数组中的项的个数是由要调用的方法的参数个数来决定的。
    如果无参数,则new Type[]{},使Type数组中的项个数为0。



    1️⃣ 被调用的方法

    在这里插入图片描述



    2️⃣ 反射方法

        private void CallVirtualMethod()
        {
            //Doozy.Runtime.UIManager.Containers.UIContainer:反射的类,如果有命名空间就需要加上。
            Type tarClass = GetType("Doozy.Runtime.UIManager.Containers.UIContainer");
            Component tarComponent = GetComponent(tarClass);
            MethodInfo tarMethod = tarClass.GetMethod("Hide", new Type[] { });
            tarMethod.Invoke(tarComponent, null);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8





    大家还有什么问题,欢迎在下方留言!


    在这里插入图片描述
    如果你有 技术的问题 项目开发

    都可以加下方联系方式

    和我聊一聊你的故事🧡

  • 相关阅读:
    业务限制模型例题
    基于轻量级yolov5的无人机航拍场景下的森林火情检测识别系统
    Android简易音乐重构MVVM Java版-LiveData+用户登录+http模块(十)
    图书管理系统
    【原创】基于SSM的医院预约挂号系统(医院预约挂号系统毕设源代码)
    【生命的分支:揭秘二叉树的神奇编码】
    攻防世界----easytornado笔记
    推荐系统的数据流
    请求和响应的概述
    【知识积累】关于解决生产Limit导出重复数据的心路历程
  • 原文地址:https://blog.csdn.net/weixin_38239050/article/details/127124163