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

2021-06-11 12:04

阅读:492

标签:cat   通过   耦合   develop   dispose   handler   demo   ble   handle   

C# 反射(Reflection)

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

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

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

优缺点

优点:

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

缺点:

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

以上来自菜鸟教程解释

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

 

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

技术图片

技术图片

 

代码部分

技术图片From1.cs
技术图片From1.Designer.cs技术图片
//Form1.Designer.cs
namespace BojayUI
{
    partial class Form1
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

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

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        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;
    }
}

技术图片技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

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

 

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

标签:cat   通过   耦合   develop   dispose   handler   demo   ble   handle   

原文地址:https://www.cnblogs.com/YangARTuan/p/10564203.html


评论


亲,登录后才可以留言!