C# 利用反射完成計算器可擴展功能

一個主要的窗體程序,兩個輸入框,一個label using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using ProOperation; using ProFactory; namespace ProOperation { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //首先讀取配置文件 string[] lines = File.ReadAllLines("Config.txt", Encoding.Default); int x = 139; int y = 154; foreach (string item in lines) { //有幾條數據,就建立幾個按鈕 Button btn = new Button(); btn.Location = new Point(x,y); btn.Size = new Size(75, 23); x += 80; btn.Text = item; btn.Click += new EventHandler(btn_Click); this.Controls.Add(btn);//添加到窗體上  } } void btn_Click(object sender, EventArgs e) { Button btn = sender as Button; int n1 = Convert.ToInt32(textBox1.Text.Trim()); int n2 = Convert.ToInt32(textBox2.Text.Trim()); //得到簡單工廠提供的父類對象 Operation oper = Factory.GetOper(btn.Text, n1, n2); if (oper != null) { label1.Text = oper.GetResult().ToString(); } else { MessageBox.Show("並無此運算符"); } } } } 四個類庫產生dll using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProOperation; namespace ProAdd { public class Add : Operation { //默認是調用的是無參構造。若是有參的話,就要這樣調用,將參數,傳給父類 public Add(int n1, int n2) : base(n1, n2){} public override string Type { get { return "+"; } } public override int GetResult() { return this.NumberOne + NumberTwo; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProOperation; using System.Reflection; using System.IO; namespace ProFactory { public class Factory { //工廠 返回父類, 可是其實父類中裝的是子類對象 public static Operation GetOper(string type, int n1, int n2) { Operation oper = null; //將前面add生成的dll文件放在debug下的plug文件夾下 //得到當前的路徑,當前的文件夾的路徑 string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Plug"); //得到path路徑下的全部文件的全路徑 string[] files = Directory.GetFiles(path,"*.dll"); //遍歷files,加載全部的程序集文件 foreach (string item in files) { //動態的得到程序集 Assembly ass = Assembly.LoadFile(item); //ass.GetType()得到指定的程序 //ass.GetTypes() 得到全部的程序,無論是否是public //得到程序集文件中全部公開的數據類型 Type[] types = ass.GetExportedTypes(); foreach (Type tt in types) { //判斷當前數據類型是不是咱們須要的數據類型 //tt是不是opeartion的子類,而且是抽象的 if (tt.IsSubclassOf(typeof(Operation)) && !tt.IsAbstract) { //建立子類對象 賦值給oper,返回值是object //object o = Activator.CreateInstance(tt,n1,n2); oper = (Operation)Activator.CreateInstance(tt, n1, n2); if (type == oper.Type) { return oper; } else { oper = null; } } } } return oper; } } } 父類 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProOperation { public abstract class Operation { public int NumberOne { get; set; } public int NumberTwo { get; set; } //構造方法沒有返回值,連void 都不要 public Operation(int n1, int n2) { this.NumberOne = n1; this.NumberTwo = n2; } //記錄子類的計算類型 +-*/ public abstract string Type { get; } //得到結果 public abstract int GetResult(); } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using ProOperation; namespace ProSub { public class Sub : Operation { public Sub(int n1, int n2) : base(n1, n2) { } public override string Type { get { return "-"; } } public override int GetResult() { return this.NumberOne - this.NumberTwo; } } }
相關文章
相關標籤/搜索