MEF Import/Export Methods

Jul 23, 2009 at 8:40 PM

I just want to export and import methods with no parameters. How would I go about doing this?

Thanx

Jul 23, 2009 at 9:42 PM

You should be able to export Action.

Regards

Glenn

From: Billl [mailto:notifications@codeplex.com]
Sent: Thursday, July 23, 2009 1:41 PM
To: Glenn Block
Subject: MEF Import/Export Methods [MEF:63363]

From: Billl

I just want to export and import methods with no parameters. How would I go about doing this?

Thanx

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 24, 2009 at 1:25 PM

Could you possibly give an example? The progranmming guide isn't helping much (on a side note, I'm using VB.Net, but I do know C# as well)

Jul 24, 2009 at 5:23 PM

Here you go Bill

Imports System.ComponentModel.Composition

Imports System.ComponentModel.Composition.Hosting

Module Module1

    Sub Main()

        Dim Catalog As New AssemblyCatalog(GetType(Module1).Assembly)

        Dim Container As New CompositionContainer(Catalog)

        Dim UsesDoIt As UsesDoIt

        UsesDoIt = Container.GetExportedValue(Of UsesDoIt)()

        Console.ReadLine()

    End Sub

End Module

Public Delegate Sub DoIt2()

Public Class DoSomeWork

    <Export("DoIt1")> _

    Public Sub DoIt1()

        Console.WriteLine("Done 1")

    End Sub

    <Export(GetType(DoIt2))> _

    Public Sub DoIt2()

        Console.WriteLine("Done 2")

    End Sub

End Class

<Export()> _

Public Class UsesDoIt

    <ImportingConstructor()> _

    Public Sub New(<Import("DoIt1")> ByVal DoIt1 As Action, ByVal DoIt2 As DoIt2)

        DoIt1()

        DoIt2()

    End Sub

End Class

The code above illustrates two different approaches to exporting methods. DoSomeWork exports two methods. DoIt1 is an action that is assigned a string contract. Doit2 it exported using a custom Delegate (DoIt2) that matches the signature of the method being exported.

UsesDoIt then imports in its constructor DoIt1 and DoIt2 and invokes them. By default when you use an importing constructor we derive the contract name for each parameter from its type, so normally the Import attribute on the parameters is not required. In the case of DoIt1 however, the contract is different than the type, so we need to specify it explicitly.

HTH

Glenn

From: Billl [mailto:notifications@codeplex.com]
Sent: Friday, July 24, 2009 6:25 AM
To: Glenn Block
Subject: Re: MEF Import/Export Methods [MEF:63363]

From: Billl

Could you possibly give an example? The progranmming guide isn't helping much (on a side note, I'm using VB.Net, but I do know C# as well)

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 24, 2009 at 5:39 PM

Thanks!

Jul 24, 2009 at 6:18 PM

How would I import them without putting them into a constructor? (Like as a delegate or method itself) I dont think I can use a property because the methods have no parameters.

Jul 24, 2009 at 10:54 PM

You can use property imports or field imports, for example, below I am using setter only properties.

<Export()> _

Public Class UsesDoItProperties

    Private _DoIt1 As Action

    <Import("DoIt1")> _

    Public WriteOnly Property DoIt1() As Action

        Set(ByVal value As Action)

            _DoIt1 = value

            _DoIt1()

        End Set

    End Property

    Private _DoIt2 As DoIt2

    <Import()> _

    Public WriteOnly Property DoIt2() As DoIt2

        Set(ByVal value As DoIt2)

            _DoIt2 = value

            _DoIt2()

        End Set

    End Property

End Class

From: Billl [mailto:notifications@codeplex.com]
Sent: Friday, July 24, 2009 11:21 AM
To: Glenn Block
Subject: Re: MEF Import/Export Methods [MEF:63363]

From: Billl

How would I import them without putting them into a constructor? (Like as a delegate or method itself) I dont think I can use a property because the methods have no parameters.

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Jul 27, 2009 at 2:37 PM

Problem Solved!

Thanx!