• C#反射调用程序集中的方法


    将dll注入进行到进程之后获取程序集信息及反射调用方法,记录一下备忘。

    获取程序集集合,找出需要用的那个
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    Assembly assembly = assemblies.Where(p => p.FullName.Contains("XXXXX")).FirstOrDefault();
    Type[] types = assembly.GetTypes();

    //调用XXXXX.ClassName中的MethodName方法
    Type type = assembly.GetType("XXXXX.ClassName");
    object obj = Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, null, null);
    MethodInfo methodRunCom = type.GetMethod("MethodName", BindingFlags.NonPublic | BindingFlags.Instance);
    object objResult = methodRunCom.Invoke(obj, null);
    string ret2 = objResult == null ? string.Empty : objResult.ToString(); //把返回处理成自己需要的格式

    //获取XXXXX.ClassName中的控件
    Type type = assembly.GetType("XXXXX.ClassName");
    MethodInfo[] infos = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
    object obj = Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, null, null);
    //控件处理
    System.Windows.Forms.TextBox textBox = (System.Windows.Forms.TextBox)type.GetField("TextBoxName", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);
    textBox.Text = "123456789";
    System.Windows.Forms.RadioButton radioButton = (System.Windows.Forms.RadioButton)type.GetField("RadioButtonName", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);
    radioButton.Checked = false;
    //调用click
    MethodInfo btnOKMethodInfo = type.GetMethod("btnXXX_Click", BindingFlags.Instance | BindingFlags.NonPublic);
    object[] param = new object[] { new object(), new EventArgs() };
    object objResult = btnOKMethodInfo.Invoke(obj, param);

    //获取属性值
    Type type = assembly.GetType("XXX.ClassName");
    MethodInfo methodInfo = type.GetMethod("CreateInstance");
    object obj = methodInfo.Invoke(null, null);
    string propertyName = "PropertyName";
    IEnumerable<PropertyInfo> property = from pi in obj.GetType().GetProperties() where pi.Name.ToLower() == propertyName.ToLower() select pi;
    object propertyValueObj = property.First().GetValue(obj, null);
    string value = propertyValueObj.ToString();
  • 相关阅读:
    Jquery 跨域请求JSON数据问题
    js定时器实现图片轮播
    Redis数据一致性
    Redis缓存击穿、缓存穿透、缓存雪崩
    数据库连接池druid连接mysql数据库‘链路断开’问题
    Mysql启动错误: Can’t create test file xxx lower-test
    DB2-表空间
    DB2-Schema
    DB2-数据库
    DB2-实例
  • 原文地址:https://www.cnblogs.com/jiayan1578/p/14003008.html
Copyright © 2020-2023  润新知