標籤:winform blog http io ar sp for on 2014
記得有個winform利用反射做的可擴充筆記本,閑來無事,便用wpf也搞了個可擴充記事本,可用介面動態擴充功能,較簡單,以便參考:
目錄結構如下:
MainWindow.xaml為主功能介面,FunctionExpand提供擴充介面;
主要功能代碼如下:
private void MakeFun()
{
string location = this.GetType().Assembly.Location;
string dllPath = System.IO.Path.GetDirectoryName(location) + "\\plugs";
string[] strDllPath = System.IO.Directory.GetFiles(dllPath, "*.dll");
foreach (var item in strDllPath)
{
Assembly ass = Assembly.LoadFile(item);
Type[] types = ass.GetExportedTypes();
Type plugType = typeof(IPlug);
foreach (var t in types)
{
//判斷是否實現IPlug介面
if (plugType.IsAssignableFrom(t))
{
//根據外掛程式類型建立外掛程式對象
IPlug iplugObj = Activator.CreateInstance(t) as IPlug;
//建立外掛程式按鈕
MenuItem menu = new MenuItem();
menu.Header = t.Name;
this.expandTool.Items.Add(menu);
menu.Click += menu_Click;
menu.Tag = iplugObj;
}
}
}
}
void menu_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = sender as MenuItem;
IPlug iplug = menuItem.Tag as IPlug;
tb_Text.Text = iplug.ProcessText(tb_Text.Text);
}
效果如下:
現把代碼分享出來,有感興趣的朋友可以研究下: 源碼下載
轉載請註明來自:閃閃的幸運星
原文地址:http://www.cnblogs.com/dongyang
如若轉載,請保留原文地址。謝謝合作。
wpf做的可擴充記事本