かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

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

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

その2で簡単なHello worldを作りました。
その3では、このHello worldをちょっと改良して、めんどくさかったクラスのインスタンスをコンテナに登録する部分と、DIコンテナチックに実装との間に一枚インターフェースを挟むようにしてみようと思います。

適当にコンソールアプリケーションを作成して、System.ComponentModel.Composition.dllを参照に追加します。

まず最初に、クラスのインスタンスを登録する部分を改善しようと思います。コンテナがどういう風にクラスのインスタンスを登録していくかを指定するものとして、~Catalogというものがあります。

その中に、System.ComponentModel.Composition.Hosting.AssemblyCatalogというものがあります。これは、指定したアセンブリの中にあるクラス群を自動で登録してくれるかわいい奴です。

ということでMainを、それをつかってコンテナを作成するように書きます。

using System.ComponentModel.Composition.Hosting;

namespace MEFSample2
{
    class Program
    {
        static void Main(string[] args)
        {
            // アセンブリからクラス登録してね
            var catalog = new AssemblyCatalog(typeof(Program).Assembly);
            // catalogを使用してコンテナを初期化
            var container = new CompositionContainer(catalog);
        }
    }
}

次に、GreeterとGreeterApp(前のエントリではApplicationって書いてたけどうつのがめんどくさくなったので短いAppにしました)を作成します。
各々、IGreeterとIGreetAppインターフェースと、それを実装するGreeterImplとGreetAppImplクラスがあります。

中身は対して変わりませんが、Export属性とかは、実装クラスのほうにつけて、Export属性の引数にインターフェースの型を指定します。
これでコンテナにIGreeterのインスタンス下さいと、問い合わせるとGreeterImplのインスタンスをくれるようになります。

IGreeterインターフェースとGreeterImplクラス

using System.ComponentModel.Composition;

namespace MEFSample2
{
    // 挨拶をする人が実装すべきインターフェース
    public interface IGreeter
    {
        string Greet();
    }

    // IGreeterの実装 IGreeterインターフェースをキーにしてExportする
    [Export(typeof(IGreeter))]
    public class GreeterImpl : IGreeter
    {
        // 単純にHello worldを返す
        public string Greet()
        {
            return "Hello world";
        }
    }
}

IGreetAppインターフェースとGreetAppImplクラス

using System;
using System.ComponentModel.Composition;

namespace MEFSample2
{
    // 挨拶アプリケーションの実装すべきインターフェース
    public interface IGreetApp
    {
        void Run();
    }

    // IGreetAppの実装 IGreetAppをキーにしてExportする
    [Export(typeof(IGreetApp))]
    public class GreetAppImpl : IGreetApp
    {
        // IGreeterをImportする
        [Import]
        public IGreeter Greeter { get; set; }

        public void Run()
        {
            // ちゃんとGreeterにインスタンスが設定されてれば挨拶が表示されるはず
            Console.WriteLine(this.Greeter.Greet());
        }
    }
}

何処にExportとImportがついてるかがポイントです。
さて、最後に、Mainを実装します。コンテナからIGreetAppのインスタンスを取得してRunメソッドを呼び出します。

using System.ComponentModel.Composition.Hosting;

namespace MEFSample2
{
    class Program
    {
        static void Main(string[] args)
        {
            // アセンブリからクラス登録してね
            var catalog = new AssemblyCatalog(typeof(Program).Assembly);
            // catalogを使用してコンテナを初期化
            var container = new CompositionContainer(catalog);

            // 実行!
            var app = container.GetExportedObject<IGreetApp>();
            app.Run();
        }
    }
}

だいぶスッキリしました。

投稿日時 : 2009年7月1日 13:01

Feedback

# aQWyYmNSYElISGZI 2011/12/27 19:07 http://www.spytown.com/

Current blog, fresh information, I read it from time to time!!...

# SvsaudvBgWbVXk 2011/12/29 20:28 http://www.healthinter.org/health/page/protonix.ph

Cool:) I would say say it exploded my brain..!

# NSWHUPKgEquMtkYbS 2012/01/07 8:48 http://www.luckyvitamin.com/m-153-garden-of-life

Thanks for all the answers:) In fact, learned a lot of new information. Dut I just didn`t figure out what is what till the end!...

# Burberry Tie 2012/10/25 8:16 http://www.burberryoutletonlineshopping.com/burber

Some genuinely fantastic posts on this website, thanks for contribution. "My salad days, When I was green in judgment." by William Shakespeare.
Burberry Tie http://www.burberryoutletonlineshopping.com/burberry-ties.html

# cheap burberry bags 2012/10/27 22:09 http://www.burberryoutletonlineshopping.com/burber

Some really excellent posts on this web site , thanks for contribution.
cheap burberry bags http://www.burberryoutletonlineshopping.com/burberry-tote-bags.html

# burberry bag 2012/10/28 13:47 http://www.burberryoutletscarfsale.com/burberry-ba

You are my breathing in, I own few blogs and sometimes run out from brand :). "Never mistake motion for action." by Ernest Hemingway.
burberry bag http://www.burberryoutletscarfsale.com/burberry-bags.html

# Nike Schehe 2012/10/30 21:05 http://www.nikefree3runschuhe.com/

If you require a certain accounting of cost, count relations.
Nike Schehe http://www.nikefree3runschuhe.com/

# cheap tie 2012/10/31 19:29 http://www.burberrysalehandbags.com/burberry-ties.

hello!,I love your writing so much! share we be in contact extra approximately your post on AOL? I need an expert in this area to solve my problem. Maybe that's you! Having a look forward to peer you.
cheap tie http://www.burberrysalehandbags.com/burberry-ties.html

# burberry watches on sale 2012/10/31 19:29 http://www.burberrysalehandbags.com/burberry-watch

Hello, Neat post. There is an issue with your website in web explorer, would check this… IE nonetheless is the market chief and a big component to other folks will pass over your fantastic writing because of this problem.
burberry watches on sale http://www.burberrysalehandbags.com/burberry-watches.html

# burberry womens shirts 2012/10/31 19:29 http://www.burberrysalehandbags.com/burberry-women

I love the efforts you have put in this, regards for all the great articles.
burberry womens shirts http://www.burberrysalehandbags.com/burberry-womens-shirts.html

# cheap burberry bags 2012/10/31 19:30 http://www.burberrysalehandbags.com/burberry-tote-

I do consider all the concepts you've presented in your post. They are really convincing and will certainly work. Still, the posts are too short for beginners. May you please lengthen them a bit from next time? Thanks for the post.
cheap burberry bags http://www.burberrysalehandbags.com/burberry-tote-bags.html

# mens shirts 2012/10/31 19:30 http://www.burberrysalehandbags.com/burberry-men-s

I have not checked in here for some time because I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my daily bloglist. You deserve it friend :)
mens shirts http://www.burberrysalehandbags.com/burberry-men-shirts.html

# Awesome! Its in fact amazing paragraph, I have got much clear idea about from this article. 2018/09/30 14:31 Awesome! Its in fact amazing paragraph, I have got

Awesome! Its in fact amazing paragraph, I have got much clear idea about from this article.

# What's up colleagues, good article and fastidious urging commented at this place, I am genuinely enjoying by these. 2018/10/07 22:37 What's up colleagues, good article and fastidious

What's up colleagues, good article and fastidious urging commented at this place,
I am genuinely enjoying by these.

# obviously like your web-site but you need to test the spelling on several of your posts. A number of them are rife with spelling issues and I to find it very troublesome to tell the truth however I'll surely come again again. 2018/10/19 6:06 obviously like your web-site but you need to test

obviously like your web-site but you need to test the spelling on several of your posts.
A number of them are rife with spelling issues and I to find it very troublesome to
tell the truth however I'll surely come again again.

# Good way of telling, and pleasant paragraph to obtain facts on the topic of my presentation subject matter, which i am going to deliver in academy. 2018/11/14 1:29 Good way of telling, and pleasant paragraph to obt

Good way of telling, and pleasant paragraph to obtain facts on the
topic of my presentation subject matter, which i am going
to deliver in academy.

# Hi, its good piece of writing about media print, we all know media is a great source of facts. 2019/03/23 18:35 Hi, its good piece of writing about media print, w

Hi, its good piece of writing about media print, we all know
media is a great source of facts.

# Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to write. pof natalielise 2019/07/26 16:47 Article writing is also a excitement, if you be a

Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to
write. pof natalielise

# Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to write. pof natalielise 2019/07/26 16:48 Article writing is also a excitement, if you be a

Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to
write. pof natalielise

# Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to write. pof natalielise 2019/07/26 16:49 Article writing is also a excitement, if you be a

Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to
write. pof natalielise

# Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to write. pof natalielise 2019/07/26 16:50 Article writing is also a excitement, if you be a

Article writing is also a excitement, if you be acquainted with then you can write or else it is difficult to
write. pof natalielise

# I'm truly enjoying the design and layout of your website. It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Exceptional work! 2019/09/17 6:19 I'm truly enjoying the design and layout of your

I'm truly enjoying the design and layout of your website.
It's a very easy on the eyes which makes it much more enjoyable
for me to come here and visit more often. Did you hire out a
designer to create your theme? Exceptional work!

# It's perfect time to make some plans for the long run and it is time to be happy. I have read this publish and if I may just I wish to counsel you some attention-grabbing things or suggestions. Perhaps you can write subsequent articles referring to th 2022/12/03 6:37 It's perfect time to make some plans for the long

It's perfect time to make some plans for the long run and it
is time to be happy. I have read this publish and if I may just
I wish to counsel you some attention-grabbing things or suggestions.
Perhaps you can write subsequent articles referring to this article.
I wish to learn even more things approximately it!

# over the counter sleeping pills 2023/01/27 2:44 Jamessop

https://over-the-counter-drug.com/# over the counter yeast infection treatment

# stromectol cost 2023/02/07 7:06 Kevinfot

Prescription Drug Information, Interactions & Side. Generic Name.
https://stromectolst.com/# ivermectin 5 mg
Everything what you want to know about pills. Prescription Drug Information, Interactions & Side.

# cost of cheap clomid tablets 2023/02/14 8:19 AaronEvazy

Read information now. What side effects can this medication cause?
https://clomiphenes.com cost of generic clomid tablets
Prescription Drug Information, Interactions & Side. Everything what you want to know about pills.

# best tadalafil generic 2023/03/04 10:37 Albertglync

Read information now. Read here.
https://tadalafil1st.online/# generic tadalafil medication
Everything what you want to know about pills. drug information and news for professionals and consumers.

# pain medications without a prescription - https://cheapdr.top/# 2023/04/03 15:20 Dikolipo

pain medications without a prescription - https://cheapdr.top/#

# where can i get doxycycline - https://doxycyclinesale.pro/# 2023/04/22 4:02 Doxycycline

where can i get doxycycline - https://doxycyclinesale.pro/#

# online prednisone - https://prednisonesale.pro/# 2023/04/22 15:08 Prednisone

online prednisone - https://prednisonesale.pro/#

# Misoprostol 200 mg buy online - https://cytotecsale.pro/# 2023/04/29 4:39 Cytotec

Misoprostol 200 mg buy online - https://cytotecsale.pro/#

# over the counter urinary tract infection meds https://overthecounter.pro/# 2023/05/08 22:43 OtcJikoliuj

over the counter urinary tract infection meds https://overthecounter.pro/#

# over the counter antidepressant 2023/05/10 10:55 Gregorysew

http://overthecounter.pro/# over the counter oral thrush treatment

# ed pills otc 2023/05/13 20:17 MichaelExcup

http://edpills.pro/# male erection pills

# top rated ed pills 2023/05/14 19:10 Mickeynix

https://edpills.pro/# cheap ed pills

# natural ed medications 2023/05/15 23:04 MichaelExcup

http://pillswithoutprescription.pro/# trusted canadian pharmacies

# no prescription pharmacies https://pillswithoutprescription.pro/# 2023/05/16 9:31 PillsPro

no prescription pharmacies https://pillswithoutprescription.pro/#

# ed treatment pills 2023/05/16 16:00 MichaelExcup

http://pillswithoutprescription.pro/# canadian pharmacies shipping to usa

# cytotec pills buy online 2023/06/07 5:17 Davidodota

http://prednisonepills.pro/# generic prednisone tablets

# men's ed pills 2023/06/28 18:48 Jamesweeva

https://sildenafilpills.pro/# sildenafil prescription nz

# generic sildenafil 50 mg 2023/06/29 12:39 DavidPaync

https://edpill.pro/# male ed pills

# cheapest ed pills online 2023/06/30 0:27 Jamesweeva

http://edpill.pro/# ed drug prices

# sildenafil generic mexico 2023/06/30 21:51 DavidPaync

https://edpill.pro/# ed pills cheap

# ï»¿paxlovid https://paxlovid.pro/# - paxlovid 2023/07/03 3:56 Paxlovid

paxlovid https://paxlovid.pro/# - paxlovid

# paxlovid buy https://paxlovid.life/# paxlovid buy 2023/07/26 6:15 Paxlovid

paxlovid buy https://paxlovid.life/# paxlovid buy

# buy misoprostol over the counter https://cytotec.ink/# - cytotec pills online 2023/07/27 1:12 PillsFree

buy misoprostol over the counter https://cytotec.ink/# - cytotec pills online

# ed medication online https://edpillsotc.store/# - ed medications online 2023/10/08 1:09 EdPills

ed medication online https://edpillsotc.store/# - ed medications online

# valtrex online canada https://valtrex.auction/ valtrex generic sale 2023/10/24 22:07 Valtrex

valtrex online canada https://valtrex.auction/ valtrex generic sale

# buy doxycycline without prescription uk https://doxycycline.forum/ buy doxycycline cheap 2023/11/25 13:12 Doxycycline

buy doxycycline without prescription uk https://doxycycline.forum/ buy doxycycline cheap

# farmaci senza ricetta elenco https://farmaciait.pro/ farmaci senza ricetta elenco 2023/12/04 10:12 Farmacia

farmaci senza ricetta elenco https://farmaciait.pro/ farmaci senza ricetta elenco

タイトル  
名前  
Url
コメント