zoukankan      html  css  js  c++  java
  • 设计支持加载项的应用程序

         构建可扩展的应用程序时,接口是中心,可使用基类代替接口,但接口通常是首选,因为它允许开发人员选择

     自己的基类,本文主要通过一个示例来讨论无缝加载和使用别人创建的类型

     一,步骤

          1.创建一个“宿主SDK”程序集,它定义了一个接口,接口的方法作为宿主应用程序与加载项之间的通信机制使用。

          2.加载项开发人员在自己的加载项程序集中定义自己的类型

          3.创建一个单独的“宿主应用程序”

    二,实例

         0.代码结构

           

             1.Wintellect.HostSDK代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Wintellect.HostSDK
    {
        public interface IAddIn
        {
            void Print();
        }
    }
    Wintellect.HostSDK

             2.AddInTypes代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Wintellect.HostSDK;
    
    namespace AddInTypes
    {
        public class AddIn_A:IAddIn
        {
    
            public void Print()
            {
                Console.WriteLine("调用对象为:"+this.ToString());
            }
    
            public override string ToString()
            {
                return "AddInTypes程序集中的AddIn_A类中的Print方法";
            }
        }
    
        public class AddIn_B : IAddIn
        {
    
            public void Print()
            {
                Console.WriteLine("调用对象为:" + this.ToString());
            }
    
            public override string ToString()
            {
                return "AddInTypes程序集中的AddIn_B类中的Print方法";
            }
        }
    }
    AddInTypes

             3.WintellectInvokeTest代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.IO;
    using Wintellect.HostSDK;
    
    namespace WintellectInvokeTest
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Assembly TempAssemly = null;
                IAddIn TempIAddIn = null;
                List<Type> TargetTypes = new List<Type>();
                String AddInDir = GetCurrentDirectory();
                String[] AddInAssemblies = Directory.GetFiles(AddInDir, "*.dll");
    
                foreach (String aia in AddInAssemblies)
                {
                    TempAssemly = Assembly.LoadFrom(aia);
                    foreach (Type t in TempAssemly.GetExportedTypes())
                    {
                        if (t.IsClass && typeof(IAddIn).IsAssignableFrom(t))
                        {
                            TargetTypes.Add(t);
                        }
                    }
                }
    
                foreach (Type t in TargetTypes)
                {
                    TempIAddIn = (IAddIn)Activator.CreateInstance(t);
                    TempIAddIn.Print();
                }
    
                Console.Read();
            }
    
            private static String GetCurrentDirectory()
            {
                return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            }
        }
    }
    WintellectInvokeTest

             备注:1.引用关系:AddInTypes和WintellectInvokeTest引用Wintellect.HostSDK

                     2.运行WintellectInvokeTest前,请将AddInTypes.dll复制到WintellectInvokeTest的Debuge目录下即可

                     3.代码过于简单,不做细讲

  • 相关阅读:
    POJ 2752 Seek the Name, Seek the Fame
    POJ 2406 Power Strings
    KMP 算法总结
    SGU 275 To xor or not to xor
    hihocoder 1196 高斯消元.二
    hihoCoder 1195 高斯消元.一
    UvaLive 5026 Building Roads
    HDU 2196 computer
    Notions of Flow Networks and Flows
    C/C++代码中的笔误
  • 原文地址:https://www.cnblogs.com/liangjie/p/3204561.html
Copyright © 2011-2022 走看看