.net使用外部程序集拓展功能

以windows窗体应用程序为例。

第一步,建立一个程序集,它包含能将插件插入可拓展windows窗体应用程序中的类型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonSnappableTypes
{
    //为可插入拓展windows窗体应用程序的所有插件提供一个多态接口
    public interface IAppFunction
    {
        void Doit();
    }
    //顺便提一个特性
    [AttributeUsage(AttributeTargets.Class)]
    public class CompanyInfoAttribute:System.Attribute
    {
        public string CompanyName{get;set;}
        public string CompanyUrl{get;set;}
    }
}

2.构建插件

需要建立一个实现IAppFunction接口的类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonSnappableTypes;

namespace CSharpSnapIn
{
    [CompanyInfo(CompanyName="My Company",CompanyUrl="www.MyCompany.com")]
    public class CsharpModule : IAppFunction
    {
        public void Doit()
        {
           MessageBox.Show("Your have just used the C# snap in!");
        }
    }
}

3.构建可拓展的Windows Froms应用程序

界面代码

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.Reflection;
using CommonSnappableTypes;

namespace MyExtendApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();           
        }

        private void snapInModuleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (dlg.FileName.Contains("CommonSnapTypes"))
                {
                    MessageBox.Show("CommonSnapTypes has no snap-ins!");
                }
                else if(!LoadExternalModule(dlg.FileName))
                {
                     MessageBox.Show("Nothing implements IAppFunction!");
                }
            }
        }
        private bool LoadExternalModule(string path)
        {
            bool found = false;
            Assembly asm = null;
            try
            {
                asm = Assembly.LoadFrom(path);//加载程序集(插件)
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }        
            var theClassTypes = from t in asm.GetTypes()
                                where t.IsClass && t.GetInterface("IAppFunction") != null
                               select t;
            foreach (var t in theClassTypes)
            {
                found = true;
                IAppFunction iApp = asm.CreateInstance(t.FullName, true) as IAppFunction;
                iApp.Doit();
                listBox1.Items.Add(t.FullName);
                DisplayCompanyData(t);
            }
            return found;
        }
        private void DisplayCompanyData(Type t)
        {
            var comInfos = from info in t.GetCustomAttributes(false)
                          where info.GetType() == typeof(CompanyInfoAttribute)
                          select info;
            foreach (CompanyInfoAttribute info in comInfos)
            {
                MessageBox.Show(info.CompanyUrl, info.CompanyName);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/wxj111/archive/2013/05/24/3096684.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Visual Studio中,要添加程序引用以便在.NET开发中使用某个程序(也称为DLL),可以按照以下步骤进行操作。 首先,打开Visual Studio并打开你的项目。在解决方案资源管理器中,展开你的项目文件夹。 接下来,右键单击“引用”文件夹,并选择“添加引用”选项。 在弹出的对话框中,你可以看到几个选项卡,包括“已安装”、“COM”、“程序”、“浏览”等。在这里,我们主要关注“程序”选项卡。 点击“程序”选项卡后,你可以浏览和选择可用的程序。此列表中显示了所有已安装的程序以及.NET框架自带的程序。 如果你找到想要添加的程序,只需选中它的复选框,并点击“确定”按钮即可添加引用。在解决方案资源管理器中的“引用”文件夹中将显示已添加的程序。 如果你想添加自己创建的程序,可以点击“浏览”选项卡,并导航到程序所在的路径。选择该程序,并点击“确定”按钮进行添加。 添加程序引用后,你就可以在你的代码中使用程序中的类、方法和其他组件了。在你的代码文件中,你可以直接使用程序中的命名空间,或者使用完整的命名空间路径。 总结起来,通过在解决方案资源管理器中的“引用”文件夹中添加程序引用,我们可以方便地在.NET开发中使用已安装或自定义的程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值