//1.定义插件接口。为了使用方便,新建一个DLL类库
using System;DLL类库是什么?03
namespace PluginInterface
{
public interface IShow
{
string Show();
}
}
//2.新建DLL类库编写插件
using System;namespace PluginA
{
//主程序调用类实现插件接口
public class PluginA : PluginInterface.IShow
{
public string Show()
{
return "I am plugin A";
}
}
}
//3.在主程序启动时加载Plugins文件夹下的所有DLL文件。为了方便管理最好在Plugins中加入插件配置文件“Plugins.xml”。配置文件怎么写?02 private void Form1_Load(object sender, System.EventArgs e)
{
try
{
//当前程序所在目录
string path = Application.StartupPath;
path = System.IO.Path.Combine(path, "Plugins"); 这句什么意思?01
//遍例Plugins目录中所有的DLL文件
foreach (string file in System.IO.Directory.GetFiles(path, "*.dll"))
{
this.cmbPlugins.Items.Add(file); cmbPlugins在哪里定义的?04 }
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} } //4.在主程序中使用插件.
private void button1_Click(object sender, System.EventArgs e)
{
try
{
//获得DLL的目录和文件名称
string asmFile = this.cmbPlugins.Text;
if (asmFile != string.Empty)
{
//利用反射,构造DLL文件的实例
System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom(asmFile);
//利用反射,从程序集(DLL)中,提取类,并把此类实例化
PluginInterface.IShow iShow = (PluginInterface.IShow)System.Activator.CreateInstance(asm.GetType("PluginA.PluginA"));//PluginA.PluginA是要实例化类的全名.(命名空间.类名)
//使用这个类的实例
this.label2.Text = iShow.Show(); }
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}正在学习写插件中。 编程环境是vs2003,项目要求是这样的。帮别人写一个中间插件传数据用的(sql2000)!
求大牛解答一下(我昨天问了没人理,我是做Java的,现在一脸茫然。哎。)
那个 反射实现动态加载PluginA 这个类 是什么意思?(我在Java里面没写过插件。惭愧)
path = System.IO.Path.Combine(path, "Plugins")这句什么意思?
this.cmbPlugins.Items.Add(file); cmbPlugins在哪里定义的?
下面这个还真没有,不过应该是个集合类,其实保存的就是字符串可以用List<string>类型代替。
Thank You! 还真是,你一说就好懂多了。
这个是新建项目的时候就选好的,CS架构的选windowscontrollibrary,BS架构的选webcontrollibrary。
做好以后可以自己建一个主程序调用来调试。我也刚开始学,只能回答点儿简单的,反射什么的查了好多资料都没弄明白。
VS平台就是有一点不好,全都封装好了,你自己都不知道调用了些什么基类,不像JAVA。
有本书叫《庖丁解牛—纵向切入Asp.net3.5控件和组件开发技术》有人说不错,我买了,正看着,共勉吧,兄弟