文档章节

C#通过反射执行C#dll所有函数

o
 osc_fmg49rzg
发布于 2019/03/20 12:44
字数 1795
阅读 19
收藏 0

精选30+云产品,助力企业轻松上云!>>>

C# 反射(Reflection)

反射指程序可以访问、检测和修改它本身状态或行为的一种能力。

程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。

您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。

优缺点

优点:

  • 1、反射提高了程序的灵活性和扩展性。
  • 2、降低耦合性,提高自适应能力。
  • 3、它允许程序创建和控制任何类的对象,无需提前硬编码目标类。

缺点:

  • 1、性能问题:使用反射基本上是一种解释操作,用于字段和方法接入时要远慢于直接代码。因此反射机制主要应用在对灵活性和拓展性要求很高的系统框架上,普通程序不建议使用。
  • 2、使用反射会模糊程序内部逻辑;程序员希望在源代码中看到程序的逻辑,反射却绕过了源代码的技术,因而会带来维护的问题,反射代码比相应的直接代码更复杂。

以上来自菜鸟教程解释

 

假如我需要让代码自动识别并执行dll里面所有的方法,这个时候就可以用到c#反射;

 

1.先用C#写个简单的UI,效果图如下:

 

代码部分

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using System.Threading;

namespace BojayUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonOpenPort_Click(object sender, EventArgs e)
        {
            this.buttonClosePort.Enabled = true;
            this.buttonOpenPort.Enabled = false;
            this.buttonOpenProfile.Enabled = true;
            //this.buttonBojay.Enabled = true;
            MessageBox.Show("OpenPort finish!");
        }

        private void buttonClosePort_Click(object sender, EventArgs e)

        {
            this.buttonClosePort.Enabled = false;
            this.buttonOpenPort.Enabled = true;
            this.buttonOpenProfile.Enabled = false;
            //this.buttonBojay.Enabled = false;
            MessageBox.Show("ClosePort finish!");
        }
        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            OpenProfileDialog.InitialDirectory = "C:\\";
            OpenProfileDialog.Filter = "Dynamic Link Library (*.dll)|*.dll|Text Document (*.txt)|*.txt|Comma-Separated Values (*.csv)|*.csv|All files (*.*)|*.*";
            if (this.OpenProfileDialog.ShowDialog() == DialogResult.OK)
            {
                this.textBoxProfilePath.Text = OpenProfileDialog.FileName;

                //save the file……
                //system.io.streamreader sr = new system.io.streamreader(openfiledialog1.filename);

                //this.textbox1.text = sr.readtoend();

                //sr.close();
            }

        }

        private void textBoxProfilePath_TextChanged(object sender, EventArgs e)
        {

        }

        private void button_start_Click(object sender, EventArgs e)
        {
            //******************执行所有类所有函数**************************//
            string path = this.textBoxProfilePath.Text;

            //string path = @"C:\Users\bojay\Desktop\ClassLibrary1\ClassLibrary1\bin\x86\Debug\ClassLibrary1.dll";

            //加载dll文件
            Assembly asm = Assembly.LoadFile(path);

            //正则表达式匹配
            string pattern = @"\w*\.";
            Match match = Regex.Match(path, pattern);
            //string[] StrArray_dll = path.Split(new string[] { "\\" }, StringSplitOptions.None);
            //string StrArray_namespace = StrArray_dll[StrArray_dll.Length - 1];
            //string[] String_class = StrArray_namespace.Split(new string[] { ".dl" }, StringSplitOptions.None);


            Type[] T = asm.GetTypes();
            for (int i = 0; i < T.Length; i++)
            {
                //********************c#****************************
                //Type type_1 = asm.GetType(T[i].Name);
                //string final_str = String_class[0] + "." + T[i].Name;
                string namespace_class = match.Value + T[i].Name;


                //获取类
                Type type = asm.GetType(namespace_class);

                //创建该类的实例
                object obj = Activator.CreateInstance(type);

                MethodInfo[] methods = type.GetMethods();
  
                try
                {
                    //c#:method.Length-4
                    //c++:method.Length-5
                    for (int index = 0; index < methods.Length - 4; index++)
                    {
                        MethodInfo mf = type.GetMethod(methods[index].Name);
                        mf.Invoke(obj, null);
                        //**************dll--function return value and parameters***************//
                        //object result = mf.Invoke(obj, null);
                        //Console.WriteLine(result);
                        //ParameterInfo[] para = me.GetParameters();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            //****************************************************************//

            //****************************************************************//
            Type temp_type = asm.GetType(match.Value + T[0].Name);

            object temp_obj = Activator.CreateInstance(temp_type);

            //FieldInfo[] pi = temp_type.GetFields();

            //get min
            FieldInfo Minlimit = temp_type.GetField("Minlimit");

            string str_min = Minlimit.GetValue(temp_obj).ToString();

            //get max
            FieldInfo Maxlimit = temp_type.GetField("Maxlimit");

            string str_max = Maxlimit.GetValue(temp_obj).ToString();

            //scope
            this.label_dll1scop.Text = str_min + " < X < " + str_max;

            //get target data
            string line;
            using (StreamReader sr = new StreamReader("common.txt"))
            {

                if ((line = sr.ReadLine()) != null)
                {
                    this.label_dll1Data.Text = line;
                }
            }

            int MIN = Convert.ToInt32(str_min);
            int MAX = Convert.ToInt32(str_max);
            int Target = Convert.ToInt32(line);
            if (MIN < Target && Target < MAX)
            {
                this.label_dll1Result.BackColor = Color.Green;
                this.label_dll1Result.Text = "PASS";
            }
            else
            {
                this.label_dll1Result.BackColor = Color.Red;
                this.label_dll1Result.Text = "Fail";
            }
            //this.label_dll1scop.Text = "1.0 < X < 100.0";
            //this.label_dll1Data.Text = "9.0";
            //this.label_dll1Result.BackColor = Color.Green;
            //this.label_dll1Result.Text = "PASS";
            //Console.ReadKey();

        }
    }
}
Form1.cs
namespace BojayUI
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.buttonOpenProfile = new System.Windows.Forms.Button();
            this.textBoxProfilePath = new System.Windows.Forms.TextBox();
            this.labelProfilePath = new System.Windows.Forms.Label();
            this.buttonClosePort = new System.Windows.Forms.Button();
            this.buttonOpenPort = new System.Windows.Forms.Button();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.button_start = new System.Windows.Forms.Button();
            this.label_dll1scop = new System.Windows.Forms.Label();
            this.label_dll1Data = new System.Windows.Forms.Label();
            this.label_dll1Result = new System.Windows.Forms.Label();
            this.label_dll1 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.OpenProfileDialog = new System.Windows.Forms.OpenFileDialog();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Location = new System.Drawing.Point(12, 38);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(776, 386);
            this.tabControl1.TabIndex = 0;
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.buttonOpenProfile);
            this.tabPage1.Controls.Add(this.textBoxProfilePath);
            this.tabPage1.Controls.Add(this.labelProfilePath);
            this.tabPage1.Controls.Add(this.buttonClosePort);
            this.tabPage1.Controls.Add(this.buttonOpenPort);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(768, 360);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "Setting";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // buttonOpenProfile
            // 
            this.buttonOpenProfile.Enabled = false;
            this.buttonOpenProfile.Location = new System.Drawing.Point(662, 73);
            this.buttonOpenProfile.Name = "buttonOpenProfile";
            this.buttonOpenProfile.Size = new System.Drawing.Size(100, 23);
            this.buttonOpenProfile.TabIndex = 4;
            this.buttonOpenProfile.Text = "OpenProfile";
            this.buttonOpenProfile.UseVisualStyleBackColor = true;
            this.buttonOpenProfile.Click += new System.EventHandler(this.buttonOpenFile_Click);
            // 
            // textBoxProfilePath
            // 
            this.textBoxProfilePath.Location = new System.Drawing.Point(132, 76);
            this.textBoxProfilePath.Name = "textBoxProfilePath";
            this.textBoxProfilePath.Size = new System.Drawing.Size(502, 21);
            this.textBoxProfilePath.TabIndex = 3;
            this.textBoxProfilePath.TextChanged += new System.EventHandler(this.textBoxProfilePath_TextChanged);
            // 
            // labelProfilePath
            // 
            this.labelProfilePath.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
            this.labelProfilePath.Location = new System.Drawing.Point(6, 75);
            this.labelProfilePath.Name = "labelProfilePath";
            this.labelProfilePath.Size = new System.Drawing.Size(100, 23);
            this.labelProfilePath.TabIndex = 2;
            this.labelProfilePath.Text = "ProfilePath";
            this.labelProfilePath.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // buttonClosePort
            // 
            this.buttonClosePort.Enabled = false;
            this.buttonClosePort.Location = new System.Drawing.Point(662, 6);
            this.buttonClosePort.Name = "buttonClosePort";
            this.buttonClosePort.Size = new System.Drawing.Size(100, 23);
            this.buttonClosePort.TabIndex = 1;
            this.buttonClosePort.Text = "ClosePort";
            this.buttonClosePort.UseVisualStyleBackColor = true;
            this.buttonClosePort.Click += new System.EventHandler(this.buttonClosePort_Click);
            // 
            // buttonOpenPort
            // 
            this.buttonOpenPort.Location = new System.Drawing.Point(6, 6);
            this.buttonOpenPort.Name = "buttonOpenPort";
            this.buttonOpenPort.Size = new System.Drawing.Size(100, 23);
            this.buttonOpenPort.TabIndex = 0;
            this.buttonOpenPort.Text = "OpenPort";
            this.buttonOpenPort.UseVisualStyleBackColor = true;
            this.buttonOpenPort.Click += new System.EventHandler(this.buttonOpenPort_Click);
            // 
            // tabPage2
            // 
            this.tabPage2.Controls.Add(this.button_start);
            this.tabPage2.Controls.Add(this.label_dll1scop);
            this.tabPage2.Controls.Add(this.label_dll1Data);
            this.tabPage2.Controls.Add(this.label_dll1Result);
            this.tabPage2.Controls.Add(this.label_dll1);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(768, 360);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "Work";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // button_start
            // 
            this.button_start.Location = new System.Drawing.Point(645, 273);
            this.button_start.Name = "button_start";
            this.button_start.Size = new System.Drawing.Size(107, 73);
            this.button_start.TabIndex = 4;
            this.button_start.Text = "Start";
            this.button_start.UseVisualStyleBackColor = true;
            this.button_start.Click += new System.EventHandler(this.button_start_Click);
            // 
            // label_dll1scop
            // 
            this.label_dll1scop.BackColor = System.Drawing.Color.Silver;
            this.label_dll1scop.Location = new System.Drawing.Point(514, 14);
            this.label_dll1scop.Name = "label_dll1scop";
            this.label_dll1scop.Size = new System.Drawing.Size(238, 27);
            this.label_dll1scop.TabIndex = 3;
            this.label_dll1scop.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label_dll1Data
            // 
            this.label_dll1Data.BackColor = System.Drawing.Color.Silver;
            this.label_dll1Data.Location = new System.Drawing.Point(326, 14);
            this.label_dll1Data.Name = "label_dll1Data";
            this.label_dll1Data.Size = new System.Drawing.Size(182, 27);
            this.label_dll1Data.TabIndex = 2;
            this.label_dll1Data.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label_dll1Result
            // 
            this.label_dll1Result.BackColor = System.Drawing.Color.Silver;
            this.label_dll1Result.Location = new System.Drawing.Point(242, 14);
            this.label_dll1Result.Name = "label_dll1Result";
            this.label_dll1Result.Size = new System.Drawing.Size(78, 27);
            this.label_dll1Result.TabIndex = 1;
            this.label_dll1Result.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label_dll1
            // 
            this.label_dll1.BackColor = System.Drawing.Color.Silver;
            this.label_dll1.Location = new System.Drawing.Point(54, 14);
            this.label_dll1.Name = "label_dll1";
            this.label_dll1.Size = new System.Drawing.Size(182, 27);
            this.label_dll1.TabIndex = 0;
            this.label_dll1.Text = "GetValue_FromDll1Text";
            this.label_dll1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.label1.Location = new System.Drawing.Point(12, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(776, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Bojay Common UI V1.0";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(12, 426);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(776, 23);
            this.label2.TabIndex = 2;
            this.label2.Text = "Developed by Tony";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // OpenProfileDialog
            // 
            this.OpenProfileDialog.FileName = "OpenProfileDialog";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.tabControl1);
            this.Name = "Form1";
            this.Text = "DEMO_UI";
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage1.PerformLayout();
            this.tabPage2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.TabPage tabPage1;
        private System.Windows.Forms.TabPage tabPage2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button buttonOpenPort;
        private System.Windows.Forms.Button buttonClosePort;
        private System.Windows.Forms.Button buttonOpenProfile;
        private System.Windows.Forms.TextBox textBoxProfilePath;
        private System.Windows.Forms.Label labelProfilePath;
        private System.Windows.Forms.OpenFileDialog OpenProfileDialog;
        private System.Windows.Forms.Label label_dll1;
        private System.Windows.Forms.Label label_dll1scop;
        private System.Windows.Forms.Label label_dll1Data;
        private System.Windows.Forms.Label label_dll1Result;
        private System.Windows.Forms.Button button_start;
    }
}
From1.Designer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BojayUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
Program.cs

 

2.C#写个dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ClassLibrary1
{
    public class Common
    {
        public int Maxlimit = 99;
        public int Minlimit = 1;
        public int common = 0;
    }
    public class Class1:Common
    {
        public void WriteFile()
        {
            int temp = this.common+5;
            using (StreamWriter sw = new StreamWriter("common.txt"))
            {
                sw.WriteLine(temp);
            }
        }
    }
}
ClassLibrary1

 

3.运行UI,加载dll路径

 

4.执行dll所有类里面的所有方法和属性

 

5.结果pass,说明该dll里面的函数功能运行结果达到我们的要求;当然啦,这个dll里面只是简单的通过文件写读,将某个目标值进行判断而已,而且我们还需要定义某个公用的类Common,及对应公用属性Maxlimit、Minlimit、common

 

总结:

<1>dll的C#代码里的namespace命名空间必须和dll文件名一致,因为我们进行匹配的时候只是匹配dll文件名;当然,我们也可以有更好的方法,例如将dll命名空间保持不变,这样我们还不需要对dll路径进行解析

<2>解析字符串的方式可以用正则,也可以用split等较为通俗简便的方法,但可能步骤会稍微多点,建议能用正则还是用正则

<3>dll是用C#写的,可以考虑能否写个.net 平台的C++dll

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。
VBA通过C#以API方式调用JS脚本函数

http://www.cnblogs.com/Charltsing/p/JSDotNetAPI.html 在网页采集中,很多时候需要运行网站下载的某个js文件中的函数,以计算Request参数。VBA直接运行JS函数有很多办法,常见的是通过mss...

osc_urm28qtx
04/16
11
0
C#插件式开发

  记录一下C#插件式开发。   原理:主要模块【运行DLL(共享DLL)】、【界面主程序】、【插件DLL】   原理没时间写太详细,以后有机会再补充吧,先上传代码。 以下是C#DLL程序集代码,...

osc_o60il3e6
04/16
1
0
【开发者笔记】java 利用jna调用c#的dll

【开发者笔记】java 利用jna调用c#的dll https://www.cnblogs.com/wyongbo/p/jnaTest.html 一、需求阐述:   如果我们的项目利用c#开发,到了开发后期需要和java组进行合作,其中有一部分业...

osc_eeik3xla
2018/07/24
7
0
【开发者笔记】java 利用jna调用c#的dll

一、需求阐述:   如果我们的项目利用c#开发,到了开发后期需要和java组进行合作,其中有一部分业务逻辑利用c#已经code completed,那么我们可能会考虑用java来调用现成的c#dll实现需求。前...

stagebo
2017/09/06
0
0
QT调用C#写的Dll

参见: https://blog.csdn.net/weixin_42420155/article/details/81060945 C#写的dll是没有dllMain入口函数的,是一种中间语言,需要.Net运行时进行做本地化工作,因此如果要调用C#写的dll,...

osc_sgs114rq
2019/01/02
6
0

没有更多内容

加载失败,请刷新页面

加载更多

dict.items()和dict.iteritems()有什么区别?

问题: Are there any applicable differences between dict.items() and dict.iteritems() ? dict.items()和dict.iteritems()之间是否有适用的区别? From the Python docs: 从Python文档中......

法国红酒甜
55分钟前
20
0
R中“ =”和“ <-”赋值运算符有什么区别?

问题: What are the differences between the assignment operators = and <- in R? R中赋值运算符=和<-之间有什么区别? I know that operators are slightly different, as this example ......

fyin1314
今天
20
0
之间的区别 和

问题: I'm learning Spring 3 and I don't seem to grasp the functionality behind <context:annotation-config> and <context:component-scan> . 我正在学习Spring 3,并且似乎不太了解<......

javail
今天
15
0
业内首款,百度工业视觉智能平台全新亮相

本文作者:y****n 业内首款全国产化工业视觉智能平台——百度工业视觉智能平台亮相中国机器视觉展(Vision China),该平台所具有的核心AI能力完全自主可控,在质检、巡检等场景中具有高效、...

百度开发者中心
昨天
7
0
我们如何制作xkcd样式图? - How can we make xkcd style graphs?

问题: Apparently, folk have figured out how to make xkcd style graphs in Mathematica and in LaTeX . 显然,民间已经想出了如何在Mathematica和LaTeX中制作xkcd风格的图形。 Can we d......

富含淀粉
今天
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部