1.定义接口
public interface IUltraman
{
string UltramanName { get; set; }
string Name { get; set; }
string Attack();
}
2.接口的继承
编译为不同的dll文件
public class Taro : IUltraman
{
private string name = "Taro";
public string Name { get => name; set => name = value; }
private string ultramanName = "泰罗atm";
public string UltramanName { get => ultramanName; set => ultramanName = value; }
public string Attack()
{
return "Taro attack monster";
}
}
public class Ace : IUltraman
{
private string name = "Ace";
public string Name { get => name; set => name = value; }
private string ultramanName = "艾斯atm";
public string UltramanName { get=> ultramanName; set=> ultramanName = value; }
public string Attack()
{
return "Ace attack monster";
}
}
3.读取dll文件为字节流
public class FileToMemory
{
public static void ToMemory(string fullName,out byte[] fileBytes)
{
using (FileStream fs = new FileStream(fullName,FileMode.Open,FileAccess.Read))
{
fileBytes = new byte[fs.Length];
fs.Read(fileBytes, 0, fileBytes.Length);
}
}
}
4.winform中代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ultraman;
namespace SelectUltraman
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//开始监视
StartWatch();
//加载dll生成button
LoadButton();
}
private void StartWatch()
{
//获取program所在的目录
string root = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location)));
//获取全目录地址
string fullPath = Path.GetFullPath(Path.Combine(root, "Plugs"));
//文件夹监视
var watcher = new FileSystemWatcher(fullPath);
watcher.EnableRaisingEvents = true;//启动监听
//采用委托的方式
//beginInvoke是线程执行后执行的
//invoke是立刻执行的
//当文件夹中新增文件
watcher.Created += (s,e)=> { this.BeginInvoke(new FileChange(LoadButton)); };
//当文件夹中删除文件
watcher.Deleted += (s, e) => { this.BeginInvoke(new FileChange(LoadButton)); };
//当文件夹中重命名文件
watcher.Renamed += (s, e) => { this.BeginInvoke(new FileChange(LoadButton)); };
}
delegate void FileChange();
private void LoadButton()
{
//情况panel中的控件
flowLayoutPanel1.Controls.Clear();
//加载插件地址
List<string> plugPaths = LoadPlugPaths("Plugs");
IEnumerable<IUltraman> ultramens = plugPaths.SelectMany(p =>
{
Assembly plugAssembly = LoadPlugs(p);
return CreateUltraman(plugAssembly);
});
foreach (var ultraman in ultramens)
{
Button btn = new Button()
{
Text = ultraman.UltramanName,
Tag = ultraman.Name
};
btn.Click += button_Click;
flowLayoutPanel1.Controls.Add(btn);
}
}
private IEnumerable<IUltraman> CreateUltraman(Assembly plugAssembly)
{
Type[] types = plugAssembly.GetExportedTypes();
IEnumerable<Type> allTypes = types.Where(m => !m.IsAbstract);
foreach (var type in allTypes)
{
IUltraman ultraman = Activator.CreateInstance(type) as IUltraman;
yield return ultraman;
}
}
private Assembly LoadPlugs(string p)
{
FileToMemory.ToMemory(p,out byte[] data);
return Assembly.Load(data);
}
private List<string> LoadPlugPaths(string path)
{
string root = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location)));
string fullPath = Path.GetFullPath(Path.Combine(root, path));
string[] files = Directory.GetFiles(fullPath);
return files.ToList();
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string tag = button.Tag as string;
string text = GetUltraman(tag);
MessageBox.Show(text);
}
private string GetUltraman(string tag)
{
List<string> plugPaths = LoadPlugPaths("Plugs");
IEnumerable<IUltraman> ultramens = plugPaths.SelectMany(p =>
{
Assembly plugAssembly = LoadPlugs(p);
return CreateUltraman(plugAssembly);
});
var ultramen = ultramens.FirstOrDefault(m=>m.Name == tag);
if (ultramen != null)
{
return ultramen.Attack();
}
else
{
return null;
}
}
}
}
标签:string,c#,System,private,热插拔,Path,using,public,Winform From: https://www.cnblogs.com/HRDK-CADeveloper/p/17471218.html