A main form program, two input boxes, one 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)
{
First read the configuration file
String[] lines = File.ReadAllLines ("Config.txt", Encoding.default);
int x = 139;
int y = 154;
foreach (String item in lines)
{
With a few data, you create several buttons
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);//Add to form
}
}
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 ());
Get a simple factory-supplied parent class object
Operation oper = Factory.getoper (btn. Text, N1, N2);
if (oper! = null)
{
Label1. Text = oper. GetResult (). ToString ();
}
Else
{
MessageBox.Show ("Does not have this operator");
}
}
}
}
Four class libraries generate DLLs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Prooperation;
Namespace Proadd
{
public class Add:operation
{
The default is to call a parameterless construct. If there is a parameter, call it this way, pass the argument to the parent class.
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
{
//Factory Returns the parent class, but in fact the parent class is loaded with a subclass object
Public Static operation Getoper (string type, int n1, int n2)
{
& nbsp; operation oper = null;
//Put the DLL file generated by the previous add under the Plug folder under Debug
//Get the current path, the path of the current folder
string path = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "Plug");
Get the full path to all files under path
string[] files = directory.getfiles (path, "*.dll");
Traverse files, load all assembly files
foreach (string item in files)
{
Dynamic acquisition of assemblies
Assembly-Assembly.loadfile (item);
The. GetType () get the specified program
The. GetTypes () Get all the programs, whether public or not
Get all the exposed data types in the assembly file
type[] types =. Getexportedtypes ();
foreach (Type tt in types)
{
Determine if the current data type is the type of data we need
Whether TT is a subclass of Opeartion and is abstract
if (TT. IsSubclassOf (typeof (Operation)) &&!tt. IsAbstract)
{
Create subclass object assignment to Oper, return value is 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;
}
}
}
Parent class
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;}
Construction method does not return a value, not even void
Public operation (int n1, int n2)
{
This. Numberone = N1;
This. Numbertwo = n2;
}
Record the calculation type of the subclass +-*/
Public abstract string Type {get;}
Get results
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;
}
}
}
C # uses reflection to complete the calculator extendable function