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();
    
    
    
            }
        }
    }
  • 相关阅读:
    3.19 DAY2
    3.18 DAY1
    MySql Scaffolding an Existing Database in EF Core
    asp.net core 2.0 后台定时自动执行任务
    c#中枚举类型 显示中文
    fullCalendar使用经验总结
    Web APP 日期选择控件
    【转】剖析异步编程语法糖: async和await
    【转】Entity Framework 复杂类型
    【转】EF Code First 学习笔记:约定配置
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5900011.html
Copyright © 2011-2022 走看看