unity 使用C#反射获取dll中的类、调用类中的字段、方法

一、什么是反射?

反射是.NET中的重要机制,通过反射,可以在运行时获得程序或程序集中每一个类型(包括类、结构、委托、接口和枚举等)的成员和成员的信息。有了反射,即可对每一个类型了如指掌。另外我还可以直接创建对象,即使这个对象的类型在编译时还不知道。

二、反射的使用 

平时我们的写法是先引用命名空间(dll),然后new一个对象,通过这个对象调用其中的字段或方法,通过反射,我们可以不用添加dll来实现效果。

1.首先我们在C#中创建一个Testdll类 打包dll,内容如下

using System;
using System.Collections.Generic;
using System.Text;

namespace TestDllImport
{
  public class Testdll
    {
        public int fieldInfoA=111;
        public string fieldInfoB="字段B";

        public string AAA() 
        {
            return "返回值测试";

        }

        public int BBB(int a,int b) 
        {
            return a + b;
        } 
    }
}

 打包dll

 找到dll导入unity的Plugins文件夹中

dll路径:C#创建项目名称\bin\Debug\netcoreapp3.1文件夹下

 2.反射

(1)获取了dll下所有的类名

using UnityEngine;
using System;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Reflection;

public class Test : MonoBehaviour
{
    void Start()
    {
        //1.获取程序集中的所有类,找到自己想要使用的类名
        Assembly assembly = Assembly.Load("TestDllImport");//加载程序集,返回类型是一个Assembly
        Type[] typeArray = assembly.GetTypes();
        for (int i = 0; i < typeArray.Length; i++)
        {
            Debug.Log(typeArray[i].Name);//这里显示dll下所有的类名
        }
    }
}

 

 我们获取了dll下所有的类名,我们使用第二个写的Testdll类

(2)获取所有字段名称,并打印

using UnityEngine;
using System;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Reflection;

public class Test : MonoBehaviour
{
    void Start()
    {
        //1.获取程序集中的所有类,找到自己想要使用的类名
        //Assembly assembly = Assembly.Load("TestDllImport");//加载程序集,返回类型是一个Assembly
        //Type[] typeArray = assembly.GetTypes();
        //for (int i = 0; i < typeArray.Length; i++)
        //{
        //    Debug.Log(typeArray[i].Name);//这里显示dll下所有的类名
        //}

        //2.两种方式获取类
        //Type type = assembly.GetType("TestDllImport.Testdll"); //1.需要一个Assembly来获取程序集
        Type type = Type.GetType("TestDllImport.Testdll,TestDllImport"); //2.Type获取  需要 "程序集.类名,程序集"格式获取
        object obj = Activator.CreateInstance(type); //创建此类型实例

        //3.获取所有字段打印
        FieldInfo[] fieldInfo = type.GetFields();
        for (int i = 0; i < fieldInfo.Length; i++)
        {
            Debug.Log(fieldInfo[i].Name+": "+ fieldInfo[i].GetValue(obj));
        }
    }
}

结果:

 (3)方法调用

using UnityEngine;
using System;
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Reflection;

public class Test : MonoBehaviour
{
    void Start()
    {
        //1.获取程序集中的所有类,找到自己想要使用的类名
        //Assembly assembly = Assembly.Load("TestDllImport");//加载程序集,返回类型是一个Assembly
        //Type[] typeArray = assembly.GetTypes();
        //for (int i = 0; i < typeArray.Length; i++)
        //{
        //    Debug.Log(typeArray[i].Name);//这里显示dll下所有的类名
        //}

        //2.两种方式获取类
        //Type type = assembly.GetType("TestDllImport.Testdll"); //1.需要一个Assembly来获取程序集
        Type type = Type.GetType("TestDllImport.Testdll,TestDllImport"); //2.Type获取  需要 "程序集.类名,程序集"格式获取
        object obj = Activator.CreateInstance(type); //创建此类型实例

        //3.获取所有字段打印
        //FieldInfo[] fieldInfo = type.GetFields();
        //for (int i = 0; i < fieldInfo.Length; i++)
        //{
        //    Debug.Log(fieldInfo[i].Name+": "+ fieldInfo[i].GetValue(obj));
        //}

        //4.获取所有方法并调用
        MethodInfo[] mInfo = type.GetMethods(); //获取当前方法
        for (int i = 0; i < mInfo.Length; i++)
        {
            Debug.Log(mInfo[i].Name);
        }

        Debug.Log("调用AAA()方法: "+ mInfo[0].Invoke(obj, new object[0]));
        Debug.Log("调用BBB(int a,int b)方法: " + mInfo[1].Invoke(obj, new object[] {10,5 }));
    }
}

结果

猜你喜欢

转载自blog.csdn.net/qq_42345116/article/details/121695595