zoukankan      html  css  js  c++  java
  • 反射

    反射获取dll文件中的信息:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace GetInfoByDll
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"D:RuPengRupengClassLibraryTestinDebugClassLibraryTest.dll";
                Assembly ass = Assembly.LoadFile(path);
                //Type tper = ass.GetType("ClassLibraryTest.Students");
                Type[] tps1 = ass.GetExportedTypes();//获取公共的
                Type[] tps = ass.GetTypes();
                for (int i = 0; i < tps.Length; i++)
                {
                    Console.WriteLine(tps[i].Name);
                }
    
    
                Type typer = ass.GetType("ClassLibraryTest.Persons");//获取实例类型
                Object obj = Activator.CreateInstance(typer);//创建实例对象
                MethodInfo mt = typer.GetMethod("Say",System.Type.EmptyTypes);//获取无参的方法
                mt.Invoke(obj,null);//调用无参的方法
    
    
                MethodInfo mt2 = typer.GetMethod("SayHello",new Type[]{typeof(string)});//获取有参 的方法
                mt2.Invoke(obj, new object[]{"kakaxi"});//调用的方法
                Console.ReadKey();
    
    
                PropertyInfo[] ps = typer.GetProperties();//获取属性
                for (int i = 0; i < ps.Length; i++)
                {
                    if(ps[i].PropertyType.ToString()=="System.String")
                    {
                        ps[i].SetValue(obj,"kakaxi",null);//设置属性值,之前要创建实例对象
                    }
                    if (ps[i].PropertyType.ToString() == "System.Int32")
                    {
                        ps[i].SetValue(obj, 100, null);
    
                    }
                    Console.WriteLine(ps[i].Name);
                    Console.WriteLine(ps[i].GetValue(obj,null));//获取实例属性的值
                }
                Console.ReadKey();
    
    
    
            }
        }
    }
  • 相关阅读:
    第一次作业
    机器学习第一次个人作业
    第02组 Beta版本演示
    第02组 Beta冲刺(4/4)
    第02组 Beta冲刺(3/4)
    微信小程序信息会话列表删除功能
    微信小程序自定义弹窗组件
    微信小程序使用Echarts
    uni.showModal,uni.showToast使用
    Array filter() 方法
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5900011.html
Copyright © 2011-2022 走看看