かずきの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!

# Hey there! I realize this is kind of off-topic but I had to ask. Does running a well-established website such as yours require a lot of work? I'm completely new to running a blog however I do write in my journal everyday. I'd like to start a blog so I 2019/09/29 4:01 Hey there! I realize this is kind of off-topic but

Hey there! I realize this is kind of off-topic but I had to ask.
Does running a well-established website such as yours require a lot of
work? I'm completely new to running a blog however I do write in my journal everyday.
I'd like to start a blog so I can easily share my experience and feelings online.

Please let me know if you have any suggestions or tips for new aspiring bloggers.
Thankyou!

# Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks 2019/11/12 8:05 Sweet blog! I found it while browsing on Yahoo New

Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News?
I've been trying for a while but I never seem
to get there! Many thanks

# Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks 2019/11/12 8:06 Sweet blog! I found it while browsing on Yahoo New

Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News?
I've been trying for a while but I never seem
to get there! Many thanks

# Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks 2019/11/12 8:07 Sweet blog! I found it while browsing on Yahoo New

Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News?
I've been trying for a while but I never seem
to get there! Many thanks

# Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks 2019/11/12 8:08 Sweet blog! I found it while browsing on Yahoo New

Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News?
I've been trying for a while but I never seem
to get there! Many thanks

# I am not sure where you're getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for excellent information I was looking for this information for my mission. 2019/11/14 12:30 I am not sure where you're getting your info, but

I am not sure where you're getting your info, but
great topic. I needs to spend some time learning more or understanding
more. Thanks for excellent information I was looking for this information for
my mission.

# This article is genuinely a pleasant one it helps new the web visitors, who are wishing for blogging. 2019/11/15 16:26 This article is genuinely a pleasant one it helps

This article is genuinely a pleasant one it helps new the web
visitors, who are wishing for blogging.

# I do not know whether it's just me or if perhaps everyone else encountering problems with your website. It appears like some of the text within your content are running off the screen. Can someone else please comment and let me know if this is happening t 2019/12/04 8:56 I do not know whether it's just me or if perhaps e

I do not know whether it's just me or if perhaps everyone else encountering problems
with your website. It appears like some of the text within your content are running off the screen. Can someone else please comment and let me
know if this is happening to them as well?
This could be a issue with my internet browser because I've had this happen previously.
Thanks

# Having read this I believed it was rather informative. I appreciate you finding the time and energy to put this content together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was 2019/12/06 6:32 Having read this I believed it was rather informat

Having read this I believed it was rather informative.
I appreciate you finding the time and energy to put this content together.
I once again find myself personally spending a significant amount of time both reading and commenting.

But so what, it was still worthwhile!

# Having read this I believed it was rather informative. I appreciate you finding the time and energy to put this content together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was 2019/12/06 6:33 Having read this I believed it was rather informat

Having read this I believed it was rather informative.
I appreciate you finding the time and energy to put this content together.
I once again find myself personally spending a significant amount of time both reading and commenting.

But so what, it was still worthwhile!

# Having read this I believed it was rather informative. I appreciate you finding the time and energy to put this content together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was 2019/12/06 6:34 Having read this I believed it was rather informat

Having read this I believed it was rather informative.
I appreciate you finding the time and energy to put this content together.
I once again find myself personally spending a significant amount of time both reading and commenting.

But so what, it was still worthwhile!

# Having read this I believed it was rather informative. I appreciate you finding the time and energy to put this content together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was 2019/12/06 6:35 Having read this I believed it was rather informat

Having read this I believed it was rather informative.
I appreciate you finding the time and energy to put this content together.
I once again find myself personally spending a significant amount of time both reading and commenting.

But so what, it was still worthwhile!

# Hi to every , for the reason that I am really eager of reading this webpage's post to be updated regularly. It includes fastidious information. 2019/12/31 18:10 Hi to every , for the reason that I am really eage

Hi to every , for the reason that I am really eager of reading this webpage's post to be updated regularly.
It includes fastidious information.

タイトル  
名前  
Url
コメント