かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

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

過去の関連記事
[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
[C#][MEF]Managed Extensibility Framework入門 その6

MEF入門も、既に7回目になりました。
あと何個か書くネタがありますが、今回は今までと趣向を変えてCatalogについてちょっと試してみようと思います。

同一アセンブリしか駄目なの?

いきなり変なタイトルをつけましたが、今までのサンプルは、AssemblyCatalogというクラスを使ってコンテナを作成していました。このAssemblyCatalogは、引数で指定したアセンブリの中からExport属性やImport属性のついたものを自動的に収集してくれるいい奴です。

ただ、これだと1つのアセンブリからしかクラスを取得できません。
後は、第二回で使ったCompositionBatchを使って1つ1つ登録しないといけないみたいに感じてしまいますが、そんなことはありません。

ちゃんと、他にも~Catalogクラスがあります。

Catalog系クラス達

Catalog系というのも今勝手につけた言い方ですが、~Catalogという名前のクラスを4つ紹介したいと思います。

  1. System.ComponentModel.Composition.Hosting.TypeCatalog
    名前からわかるとおり、指定された型を対象にするカタログクラスです。
  2. System.ComponentModel.Composition.Hosting.AssemblyCatalog
    今までお世話になってきた、指定したアセンブリ内のクラスを対象にするカタログクラスです。
  3. System.ComponentModel.Composition.Hosting.DirectoryCatalog
    こいつが今回の大本命です。
    指定したディレクトリ内にあるアセンブリを勝手に読み込んでくれます。うわさによると、実行中に追加されたDLLも認識するとか・・・。
  4. System.ComponentModel.Composition.Hosting.AggregateCatalog
    複数のCatalogを一まとめにする縁の下の力持ち

TypeCatalog

TypeCatalogは、コンストラクタで渡したTypeを対象にするクラスです。何かフレームワークとかで決まった型を登録したいとかいうときに使いそう?

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace MEFCatalog
{
    [Export]
    public class Greeter
    {
        public string Greet()
        {
            return "Hello world";
        }
    }

    [Export]
    public class GreetApp
    {
        [Import]
        public Greeter Greeter { get; set; }

        public void Execute()
        {
            Console.WriteLine(Greeter.Greet());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // TypeCatalogにGreeterとGreetAppを登録
            var typeCatalog = new TypeCatalog(
                typeof(Greeter), typeof(GreetApp));
            // TypeCatalogでContainerを作成
            var container = new CompositionContainer(typeCatalog);

            // 後はいつもどおり
            var app = container.GetExportedObject<GreetApp>();
            app.Execute();
        }
    }
}

このコードでHello worldが表示されます。

AssemblyCatalog

これまでのサンプルの通りなので省略

AggregateCatalog

AggregateCatalogは、複数のCatalogを1まとめにするために使います。
例えば、AssemblyCatalogとTypeCatalogを1まとめにして、コンテナに渡す場合は下のようになります。

// 型A, B, Cを登録するよ
var typeCatalog = new TypeCatalog(
  typeof(A), typeof(B), typeof(C));

// 型Programのあるアセンブリ内のクラスを登録するよ
var assmCatalog = new AssemblyCatalog(
  typeof(Program).Assembly);

// typeCatalogとassmCatalogを1まとめに
var aggCatalog = new AggregateCatalog(
  typeCatalog, assmCatalog);

// typeCatalogとassmCatalogにある両方の型が登録されたコンテナの完成
var container = new CompositionContainer(aggCatalog);

CompositionContainerが、Catalogを1つしか受け取らないので、複数のCatalogを使いたいときは、AggregateCatalogで事前に1まとめにしておく必要があるようです。

 

DirectoryCatalogについては、また次回で(時間切れ)!

投稿日時 : 2009年7月5日 11:46

Feedback

# Christian Louboutin Pumps 2012/12/07 19:04 http://myclsale.webs.com/

Regards for helping out, fantastic info .

# longchamp pliage pas cher 2012/12/15 16:11 http://www.soldesacslongchamp.info/category/longch

You in fact know your current stuff...

# burberry uk sale 2012/12/15 22:47 http://www.burberryuksale.info/category/burberry-o

Looking in advance to looking at more!

# 安いエルメス 2012/12/17 21:42 http://www.hermespairs.info/category/エルメス

I want to take a new closer evaluate some of that memorabilia!

# sac michael kors 2012/12/18 2:00 http://michael-kors-canada.webnode.fr/

I don't know... there's an issue tacky in relation to owning Ferrari branded things like this.. unless therefore, you own a real Ferrari.

# isabel Marant Paris 2012/12/18 21:21 http://www.isabelmarant-stores.com

this is something we've never ever previously read.

# michael kors montre 2012/12/21 7:36 http://sac-michael-kors.webnode.fr/blog/

This is known as a really fine site blog post, im delighted I discovered it.

# college english essays g75mct 2022/09/08 22:18 Charlosmox

You expressed this adequately. https://definitionessays.com/ narrative essay for college

# legitimate paper writing service g87wym 2023/03/01 5:26 StevenGrelo


Regards, I appreciate this.
pay for a paper to be written https://service-essay.com/ buy papers online cheap

# how to write a analytical essay y326aj 2023/03/06 12:01 Gregorysaipt


Wonderful advice. With thanks!
how to write personal essay for college https://writingresearchtermpaperservice.com i need help to write an essay https://ouressays.com

# phd thesis defence i99ybh 2023/03/07 10:35 Gregorysaipt


Thanks a lot. Valuable stuff!
a good college essay https://custompaperwritingservices.com college board essays https://essaywritingserviceahrefs.com

# dissertation editing services v637fe 2023/03/08 8:54 Gregorysaipt


With thanks! Useful information.
help writing assignments https://homeworkcourseworkhelps.com websites to write essays https://essayservicehelp.com

# essay writing helper r20lcc 2023/03/09 14:24 EugeneSib


Terrific tips. With thanks!
editing essay services https://writingresearchtermpaperservice.com mba thesis https://researchpaperwriterservices.com

# writing a 5 paragraph essay r22mgj 2023/03/10 5:02 EugeneSib


Beneficial info. Thanks.
how to write a 4 paragraph essay https://essaywritinghelperonline.com websites to type essays https://service-essay.com

# how to write an illustration essay t14tsz 2023/03/10 19:20 EugeneSib


You've made the point.
how to write an expository essay https://phdthesisdissertation.com help write an essay https://bestmasterthesiswritingservice.com

# business letter writing services u93wgz 2023/03/11 2:08 Gregorysaipt


Kudos. A lot of forum posts.
thesis editor https://domyhomeworkformecheap.com dissertation study https://writinganessaycollegeservice.com

# writers resources from paragraph to essay k51fbj 2023/03/11 23:49 Gregorysaipt


Fine information. Appreciate it.
essay editing service online https://hireawriterforanessay.com good dissertation https://writingpaperforme.com

# best college essay ever y351px 2023/03/12 1:34 EugeneSib

You actually said that adequately.
persuasive writing essays https://essayservicehelp.com write me an essay https://cheapessaywriteronlineservices.com

# essays about college education s562jc 2023/03/13 6:44 EugeneSib


Nicely put, Kudos.
uc college essay prompts https://phdthesisdissertation.com writing the college essay https://buyanessayscheaponline.com

# custom writing essay service o92gze 2023/03/13 19:20 Gregorysaipt


Factor effectively applied!.
write thesis https://phdthesisdissertation.com custom essay online https://essaytyperhelp.com

# thesis and dissertation v49uwe 2023/03/13 21:23 EugeneSib


Nicely expressed of course. .
college essay formats https://studentessaywriting.com write my college essay for me https://helptowriteanessay.com

タイトル
名前
Url
コメント