• MEF插件开发案例


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

    namespace WebApplication3
    {
       
       public interface ICalculator
        {
            String Calculate(String input);
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.Composition;

    namespace WebApplication3
    {
        [Export(typeof(ICalculator))]
        public class MySimpleCalculator : ICalculator
        {
            public string Calculate(string input)
            {
                return "MySimpleCalculator 处理了" + input;

            }

        }


    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.Composition.Hosting;
    using System.ComponentModel.Composition;
    using System.Reflection;

    namespace WebApplication3
    {
        public class ComposCLass
        {
            private CompositionContainer _container;
            [Import]
            public ICalculator calculator1{set;get;}
            public ComposCLass()
            {
                Compose();
            }

            public void Compose()
            {
                var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

                _container = new CompositionContainer(catalog);
                try
                {
                    this._container.ComposeParts(this);
                }
                catch (CompositionException compositionException)
                {
                }
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    namespace WebApplication3
    {
     


        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ComposCLass classp = new ComposCLass();
                //classp.Compose();
                Response.Write(classp.calculator1.Calculate("asldhcgashdcbgalshdcblasdhc"));
            }
        }
    }

  • 相关阅读:
    option 变为不可选
    html表格 横跨多行跨多列
    ultraEdit 关键字颜色 与 字体模糊 解决办法
    更改U盘 硬盘背景
    js 函数带值。调用
    Blitz: a collection of software designed to support a universitylevel course on Operating Systems
    不错的Qt入门
    Linux命令行技巧zz
    rvalue_references
    http://channel9.msdn.com/
  • 原文地址:https://www.cnblogs.com/greefsong/p/3137766.html
Copyright © 2020-2023  润新知