かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[C#][MEF]Managed Extensibility Framework入門 その6

過去の関連記事
[C#][MEF]Managed Extensibility Framework入門 その1
[C#][MEF]Managed Extensibility Framework入門 その2
[C#][MEF]Managed Extensibility Framework入門 その3
[C#][MEF]Managed Extensibility Framework入門 その4
[C#][MEF]Managed Extensibility Framework入門 その5

前回は、MethodのExportとImportについて試してみました。
これまでの5回で、基本的なImportとExportについては出来るようになりました。そして、適切にExport属性をつけるだけで勝手に適切なImport属性がついてるプロパティに設定されるのは素敵だということを感じたと思います。

今回は、同じ条件に合致する複数のExport属性があるときのImportの仕方について見ていこうと思います。

見ていくといっても大して書くことはなくて、これまで

[Import]
public TPropertyType PropertyName { get; set; }

のように普通のプロパティにしていた部分を

[Import]
public List<TPropertyType> PropertyName { get; set; }

のようにコレクションにするだけでExportが複数ある場合にコレクションにImportされるようになります。

ということで簡単に動きを見ていきます。
とりあえず、MEFEduというコンソールアプリケーションを作って、System.ComponentModel.Composition.dllを参照に追加して、いつもどおりのコンテナの初期化のコードを書きます。

using System.ComponentModel.Composition.Hosting;

namespace MEFEdu
{
    class Program
    {
        static void Main(string[] args)
        {
            // コンテナの初期化
            var catalog = new AssemblyCatalog(
                typeof(Program).Assembly);
            var container = new CompositionContainer(catalog);
        }
    }
}

まず、GreetAppクラスを作成します。Actionのコレクションのプロパティを定義して、Import(“Greet”)という属性を付けています。ついでに、Mainのほうも、コンテナからGreetAppのインスタンスを取得して実行するようにします。

using System.ComponentModel.Composition.Hosting;
using System;
using System.ComponentModel.Composition;
using System.Collections.Generic;

namespace MEFEdu
{
    class Program
    {
        static void Main(string[] args)
        {
            // コンテナの初期化
            var catalog = new AssemblyCatalog(
                typeof(Program).Assembly);
            var container = new CompositionContainer(catalog);

            var app = container.GetExportedObject<GreetApp>();
            app.Execute();
        }
    }

    [Export]
    public class GreetApp
    {
        [Import("Greet")]
        public List<Action> GreetActions { get; set; }

        public void Execute()
        {
            // 登録されている挨拶を実行!
            foreach (var action in GreetActions)
            {
                action();
            }
        }
    }
}

GreetAppクラスのGreetActionsに登録するメソッドを定義していきます。Greeterクラスを作成して、そこにメソッドを定義してExport(“Greet”)属性をつけています。

public class Greeter
{
    [Export("Greet")]
    public void HelloWorld()
    {
        Console.WriteLine("Hello world");
    }

    [Export("Greet")]
    public void こんにちは世界()
    {
        Console.WriteLine("こんにちは世界");
    }
}

これで実行すると、複数のアクションが登録されて、実行されるのがわかります。

実行結果

Hello world
こんにちは世界

こういう風にして、アプリケーションを何か作る時に、追加処理したいポイントをDelegateのコレクションのプロパティにImport属性をつけたものにしておくと、簡単に拡張できるようになります!!

素敵だ。

投稿日時 : 2009年7月3日 12:52

Feedback

# PNC Online Login 2012/05/29 6:19 Swadiagaigh

PNC Lender http://www.onlinebankinglogon.com/
On-line Banking is the provider provided by PNC Lender to its buyers and accountholders. As a result of this services, the bank may help you:

Access your PNC Banking accounts any time from the day, from anyplace on earth.
Save on time by performing many banking functions above the web.


Banking Services Supplied by PNC-Bank On the net Banking

The Lender has supplied its customers and accountholders various facilities by way of the online world Banking company. This free-of-cost on-line banking assistance can help account holders:

Keep in contact with their finances by offering them a comprehensive summary in their PNC personalized, company and expenditure accounts.
Look at their per month statements and print and obtain the statements for that time period of around 36 months.
Access their accounts, check out their account balances and critique the latest transactions for accuracy and authenticity.
Transfer cash from 1 account to another in excess of the online. The transfer of capital can be done among two PNC Lender accounts or in between a legitimate PNC-Bank account as well as a valid account at any other money institution.
Get their charges online. This decreases chance of consumers incurring a high-quality due to delay in bills.
Preserve money and time by enabling them to pay for their bills on the net.
Toremaininformed about each and every celebration linked to their accounts and bills as a result of e-mail notifications.


Supplemental Amenities Provided by the PNC-Bank Online Banking Company

By cyberspace Banking services of PNC Bank, accountholders could also:

Adjust their individual info
Buy for checkbook
Make ask for for quit repayments and copies of deposit slips, cleared checks or account statements
Indication in for extra products and services such as Verified by Visa and PNC FYIs & Deals
Locate ATMs or branches of PNC Banks
Explore product info, tools, articles and special offers.

# www.chase.com 2012/05/30 5:46 JalkGlamn

Chase On-line login banking is amongst the greatest on line finance institutions at this time for my part. They provide competitive premiums and seriously do an incredible occupation at buidling yoru bank account. Visit http://pcinternetbanking.com/chase-online-banking/ for the look and you also can read about it for youreslf.

# XQIbpuvDAQ 2018/06/01 21:35 http://www.suba.me/

jfUUCM Thanks for sharing, this is a fantastic blog article. Awesome.

# lFeCQKtCGgGUBNT 2018/06/03 15:01 https://goo.gl/vcWGe9

I value the blog post.Much thanks again.

# pBqSSaqdonAEJrom 2018/06/04 0:16 https://topbestbrand.com/&#3588;&#3619;&am

Really enjoyed this blog.Really looking forward to read more. Awesome.

# XQIGqGhmpeNlabyIg 2018/06/04 0:49 https://topbestbrand.com/&#3629;&#3633;&am

I recommend to you to visit a site on which there are many articles on this question.

# rzjJYkQPLnHSJRDW 2018/06/04 6:02 http://narcissenyc.com/

What a great article.. i subscribed btw!

# szbsoMiOhwwaF 2018/06/04 12:08 http://www.seoinvancouver.com/

Your style is so unique in comparison to other people I ave read stuff from. Many thanks for posting when you have the opportunity, Guess I all just bookmark this web site.

# VDeYoePSZUPlxwCDtXx 2018/06/04 15:51 http://www.seoinvancouver.com/

Wow, great blog.Really looking forward to read more.

# XQnwAaKWQUcx 2018/06/04 23:31 http://www.narcissenyc.com/

Thanks for sharing, this is a fantastic post. Keep writing.

# nrRFNzHPVfCiO 2018/06/05 7:08 http://www.narcissenyc.com/

Wohh exactly what I was looking for, regards for posting.

# FVsGeWXzUqWacNZbzy 2018/06/05 10:57 http://vancouverdispensary.net/

It as not that I want to replicate your web site, but I really like the style and design. Could you let me know which design are you using? Or was it tailor made?

# FnEGgtXcviHNnBqlgXP 2018/06/05 12:49 http://vancouverdispensary.net/

It as very simple to find out any topic on web as compared to textbooks, as I found this paragraph at this web page.

# oZTzNvtmwcvO 2018/06/05 16:35 http://vancouverdispensary.net/

It is really a great and useful piece of information. I am glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

# xMZKhLuRSUtErHSj 2018/06/05 22:21 http://closestdispensaries.com/

What as up, just wanted to say, I liked this post. It was helpful. Keep on posting!

# oASkdwvkBJVRXouUoKC 2018/06/08 20:51 https://www.youtube.com/watch?v=3PoV-kSYSrs

Major thankies for the blog article.Thanks Again. Really Great. this site

# EFUDHbMgpqH 2018/06/08 21:32 http://markets.ask.com/ask/news/read/36307447

I think other web-site proprietors should take this website as an model, very clean and fantastic user friendly style and design, as well as the content. You are an expert in this topic!

# eVczPryfxFRnDZf 2018/06/08 23:20 https://topbestbrand.com/&#3593;&#3637;&am

I'а?ve learn some just right stuff here. Definitely price bookmarking for revisiting. I wonder how much effort you set to make the sort of great informative website.

# UJuHCHhmyFKH 2018/06/09 4:53 https://victorpredict.net/

This awesome blog is no doubt educating and besides diverting. I have chosen a lot of useful stuff out of it. I ad love to go back over and over again. Thanks!

# yqFUDzgZsKrjTgB 2018/06/09 5:28 http://en.wiki.lesgrandsvoisins.fr/index.php?title

Really enjoyed this post.Thanks Again. Really Great.

# WesnzOtqwMvSSBwG 2018/06/09 6:03 https://www.financemagnates.com/cryptocurrency/new

You made some good points there. I looked on the net for more info about the issue and found most people will go along with your views on this web site.

# LTtWiMTQCyXVaDKIRM 2018/06/09 6:38 http://www.seoinvancouver.com/

wow, awesome blog post.Really looking forward to read more. Want more.

# MGXOrQxUfzyV 2018/06/09 10:31 http://www.seoinvancouver.com/

Pretty! This was an incredibly wonderful article. Many thanks for providing these details.

# IdcXmsfPRenqfuT 2018/06/09 12:28 https://greencounter.ca/

website and detailed information you provide. It as good to come

# PvWWmoLivJ 2018/06/09 18:10 http://www.seoinvancouver.com/

You made some respectable points there. I looked on the internet for the issue and found most people will go along with with your website.

# eDoIGwaKFrND 2018/06/09 22:03 http://surreyseo.net

Thanks-a-mundo for the blog post.Really looking forward to read more. Want more.

# xAPNuJdDATX 2018/06/10 1:52 http://iamtechsolutions.com/

Thorn of Girl Great info might be uncovered on this website blogging site.

# wryjYUUuto 2018/06/10 11:22 https://topbestbrand.com/&#3594;&#3640;&am

YouTube consists of not just comic and humorous video lessons but also it carries learning related video lessons.

# WHNpUawsMouJT 2018/06/10 11:57 https://topbestbrand.com/&#3648;&#3626;&am

Usually I do not learn article on blogs, however I wish to say that this write-up very pressured me to take a look at and do it! Your writing taste has been surprised me. Thanks, very great article.

# gKJYIYVimsyhWQUNf 2018/06/10 12:33 https://topbestbrand.com/&#3624;&#3641;&am

Thanks-a-mundo for the blog post. Great.

# dnUicWjqRfCm 2018/06/11 18:19 https://topbestbrand.com/10-&#3623;&#3636;

Online Article Every so often in a while we choose blogs that we read. Listed underneath are the latest sites that we choose

# fpHgkcSkFubw 2018/06/11 18:54 https://topbestbrand.com/&#3607;&#3633;&am

What the best way to start up a dynamic website on a limited budget?

# wixkFYhplYsXBP 2018/06/11 19:30 https://tipsonblogging.com/2018/02/how-to-find-low

May I simply just say what a relief to find someone that truly understands what they

# inKOoaMTiy 2018/06/12 18:57 http://betterimagepropertyservices.ca/

I went over this internet site and I believe you have a lot of good information, saved to my bookmarks (:.

# MCnBaQZYUdnnPoih 2018/06/12 20:54 http://closestdispensaries.com/

Many thanks for sharing this fine piece. Very inspiring! (as always, btw)

# lcWWxHTOKVhMMPPRghe 2018/06/12 22:53 http://naturalattractionsalon.com/

Wow, great article post.Thanks Again. Awesome.

# JmQyhMyAEAbwP 2018/06/13 0:52 http://naturalattractionsalon.com/

My brother recommended I would possibly like this blog. He was entirely right. This post actually made my

# IeEgRBhJTcpIGIKAy 2018/06/13 2:50 http://www.seoinvancouver.com/

Really appreciate you sharing this post.Really looking forward to read more. Really Great.

# LrnQrMxiUtTPfKOP 2018/06/13 4:49 http://www.seoinvancouver.com/

Pretty! This has been an incredibly wonderful post. Many thanks for supplying these details.

# zallowagrUqgRBW 2018/06/13 11:25 http://www.seoinvancouver.com/

This website definitely has all of the information I wanted about this subject and didn at know who to ask.

# duJVmPScyrslzdaDJ 2018/06/13 13:21 http://www.seoinvancouver.com/

I think other web-site proprietors should take this web site as an model, very clean and excellent user friendly style and design, let alone the content. You are an expert in this topic!

# mwGyZJLCEuKrKoHue 2018/06/13 20:01 http://hairsalonvictoriabc.com

You could definitely see your skills in the work you write. The world hopes for even more passionate writers like you who are not afraid to say how they believe. Always follow your heart.

# pofBGwKloTENnSyLg 2018/06/13 21:59 https://www.youtube.com/watch?v=KKOyneFvYs8

Inspiring quest there. What occurred after? Take care!

# beGxQisnzFwlnZyE 2018/06/14 1:53 http://www.wlox.com/story/38229665/news

Thanks for sharing, this is a fantastic article.Really looking forward to read more. Keep writing.

# QAzHCFfjyzMCbCG 2018/06/15 2:26 https://www.youtube.com/watch?v=cY_mYj0DTXg

This page definitely has all of the information and facts I needed about this subject and didn at know who to ask.

# tjAFkiLyWjmmq 2018/06/15 3:06 http://buy.trafficvenuedirect.com/buy-app-traffic-

I was suggested this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are amazing! Thanks!

# ObxEUWSeyDxufG 2018/06/15 18:16 https://purdyalerts.com/contact/

I truly appreciate this post. I ave been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thanks again!

# upDLoUJyztVlHjxjyM 2018/06/15 20:19 https://topbestbrand.com/&#3648;&#3623;&am

Thanks so much for the article post. Really Great.

# ZBWzseSJYlnxNs 2018/06/16 6:54 http://sergiohgbcz.ka-blogs.com/6174307/helping-th

Once open look for the line that says #LAST LINE аАа?аАТ?б?Т€Т? ADD YOUR ENTRIES BEFORE THIS ONE аАа?аАТ?б?Т€Т? DO NOT REMOVE

# WnvJvxgQVjqohXQ 2018/06/18 15:34 https://www.techlovesstyle.com/single-post/2018/06

Simply wish to say your article is as astonishing.

# jUsiExOnbjZercZZzG 2018/06/18 18:14 https://topbestbrand.com/&#3619;&#3633;&am

pretty handy material, overall I think this is worthy of a bookmark, thanks

# LFAeBlyGQm 2018/06/18 22:15 https://ecotektermite-3.atavist.com/ecotek-termite

Major thanks for the blog post.Thanks Again. Want more.

# vAqnxEHuZqEGrX 2018/06/19 0:18 https://fxbot.market

It as hard to come by knowledgeable people about this subject, however, you sound like you know what you are talking about! Thanks

# REPxnXeCCMnBSgbSzb 2018/06/19 1:41 http://apk-smart.jigsy.com

Look forward to looking over your web page repeatedly.

# sBFmDIYBqQMC 2018/06/19 3:46 http://purity-test-questions.my-free.website/

pretty useful stuff, overall I believe this is really worth a bookmark, thanks

# qyKHpdcxuBDcegAGLb 2018/06/19 5:09 https://storia.me/@nectur/tweakbox-apk-download-fo

That is a good tip especially to those fresh to the blogosphere. Brief but very accurate information Many thanks for sharing this one. A must read article!

# JmuqYqIviBkEh 2018/06/19 7:11 https://www.graphicallyspeaking.ca/

This blog was how do I say it? Relevant!! Finally I have found something which helped me. Cheers!

# NRwbavmUAX 2018/06/19 9:12 https://www.graphicallyspeaking.ca/

My spouse and I stumbled over right here different site and believed I really should examine points out.

# NYBagCVTSWQuGuP 2018/06/19 11:52 https://www.graphicallyspeaking.ca/

Very informative blog.Really looking forward to read more. Much obliged.

# ucipHIkwwUXUNit 2018/06/19 17:57 https://rephrenothey.dreamwidth.org/

pretty beneficial stuff, overall I feel this is really worth a bookmark, thanks

# trahrWkLHOq 2018/06/19 21:22 https://www.guaranteedseo.com/

It as not that I want to duplicate your web page, but I really like the design and style. Could you let me know which style are you using? Or was it tailor made?

# EUgVOFZBLbYslLz 2018/06/19 22:02 https://www.marwickmarketing.com/

VIP Scrapebox list, Xrumer link list, Download free high quality autoapprove lists

# iFZXbnAWHoOMLOZY 2018/06/21 19:52 https://topbestbrand.com/&#3629;&#3633;&am

You have made some decent points there. I checked on the web for additional information about the issue and found most individuals will go along with your views on this site.

# czwEYvLgQc 2018/06/21 20:33 https://topbestbrand.com/&#3588;&#3619;&am

Really informative blog article.Thanks Again. Keep writing.

# cZecQLeKQGDwoYAg 2018/06/21 23:23 https://www.youtube.com/watch?v=eLcMx6m6gcQ

Im no pro, but I consider you just crafted the best point. You certainly understand what youre talking about, and I can truly get behind that. Thanks for staying so upfront and so straightforward.

# jYRdzqwshHiO 2018/06/22 17:19 https://wanelo.co/peaceloveworld

nfl jerseys has come under heavy attack for the health and safety standards it allows and the amount it pays workers abroad.

# YbNQyJskzHDZOcb 2018/06/22 18:43 https://www.youtube.com/watch?v=vBbDkasNnHo

Wonderful site. Plenty of helpful information here. I am sending it to a few buddies ans also sharing in delicious. And certainly, thanks in your effort!

# xrDihieaWqXYMUUXgE 2018/06/22 20:07 https://best-garage-guys-renton.business.site

Thanks-a-mundo for the blog article.Much thanks again. Great.

# GmReNUnuCgOCbRgpMq 2018/06/23 0:14 http://soapthai.com/

Im having a little problem. I cant get my reader to pick-up your feed, Im using msn reader by the way.

タイトル  
名前  
Url
コメント