顾锦歌——发现生活的美!

文章 分类 标签
23 11 0

公告

此处是站点公告

简易C#动态加载dll(实现插件化)

顾锦歌 2022-02-28 276 0条评论 C#

首页 / 正文

可以通过该方法来实现程序插件化。

假设A,B两个类,A类为宿主,B类为插件需要加载到A类中:

 class Program
{
    public interface IHellow
    {
        void Hellow();
    }

    static void Main(string[] args)
    {
        Assembly ass = Assembly.LoadFrom("D:/xx.dll");
        var wormMain = ass.GetTypes().FirstOrDefault(m => m.GetInterface(typeof(IHellow).Name) != null);
        var tmpObj = (IHellow)Activator.CreateInstance(wormMain);

        tmpObj.Hellow();

        Console.Read();
    }
}

namespace ClassLibrary1
{
    public class WormMain : IHellow
    {
        public void Hellow()
        {
            Console.WriteLine("Test Ok");
        }
    }
}

需要引用一下A类的项目文件,以得到接口。

如果是连同依赖项一起被载入,应该调用Assembly.LoadFrom

A类输出结果:

"Test Ok"

评论(0)

当前没有评论,还不快来留下第一个脚印吧

热门文章

最新评论

  • Jacob Hankinson

    Your go-to source for leads. We can provide business to business and business to consumer leads, custom-tailored to your needs. CustomDatabases.org

  • Otilia Drechsler

    Hi, It is with sad regret to inform you TopDataList.com is shutting down. We are ceasing operations on TopDataList.com and have made our leads available at a $149 once off fee. Visit us on TopDataList.com Regards, Otilia

  • Ervin Quinn

    Hello, from CustomData.click we are a provider of unique databases that could help your business. Please visit us at CustomData.click to see if we can help you. Regards, Ervin

  • Keri Cardus

    Hello. It is with sad regret to inform you TopDataList.com is shutting down. We have made all our databases available for you for a once off fee. Visit us on TopDataList.com

  • Beth Steele

    ZippyLeads.org is running an easter special till the 18th of April. Get all the leads you need for your company with our easter special.

  • Johan Fourie

    Hello. My name is Johan Fourie and I am looking to sell DataList.biz. We are a data company that has been in the industry for 12 years. We do around $170k/year in revenue. 1) I am looking to sell 50% of the business for $5k. 2) It would be helpful if you are knowledgeable about the Data Business. 3) I am looking for someone that is willing to take over administration, support, client relations. 4) I will continue to do the marketing for new products. 5) You will accept all future income and pay me from it. Please contact me on WhatsApp +27 72 280 1952 or my personal email: johanfourieinc@gmail.com if you are interested in this and we can have a call. Regards, Johan Fourie

  • Pam Using

    Hi, I am interested in some of your products. Please give me a call on +1 304-873-4360

  • Typecho

    欢迎加入 Typecho 大家族

日历

2025年01月

   1234
567891011
12131415161718
19202122232425
262728293031 

标签云

文章目录

推荐关键字:

上一张 下一张