MEF 6 issue

Jul 18, 2009 at 8:30 AM
Edited Jul 18, 2009 at 9:23 AM

Nevermind, I found the issue solution. I can't figure out how to delete this discussion.

Jul 18, 2009 at 10:20 AM

Hi Rick

Metadata is no longer a dictionary, rather it is the metadata view (formerly MetadataView property on Export<T,M>) that is the type that was passed for U. So the reason the code is failing, is because you are accessing a ContainsKey method on Metadata, yet the compiler does not know that one exists. In order to have the code compile, you need to pass in IDictionary(Of String, Object) as the view.

What you can do to get the code to pass, is change GetExportByMetadata to be the following signature.

   Public Function GetExportByMetadata(Of T)(ByVal name As String) As Lazy(Of T, IDictionary(Of String, Object))

        Dim RetVal As Lazy(Of T, IDictionary(Of String,Object)) = Nothing

        Try

            RetVal = _Container.GetExports(Of T, IDictionary(Of String, Object)).Where(Function(p) p.Metadata.ContainsKey(name) = True).Single()

        Catch ex As Exception

        End Try

        Return RetVal

    End Function

Better however it to use a strongly typed metadata view. So create an interface which has a ReadOnly Name property like so (forgive my vb typos if there are any)

Public Interface INameView ‘can be whatever name you want

   ReadOnly Property Name() as String

End Interface

Then you can use the following code for your method which will be more type safe. Also you don’t need to apply a special where filter, as the metadata view will automatically now filter out anything that does not have Name in its metadata.

   Public Function GetExportByMetadata(Of T)(ByVal name As String) As Lazy(Of T, INameView)

        Dim RetVal As Lazy(Of T, INameView) = Nothing

        Try

            RetVal = _Container.GetExports(Of T, INameView).Single()

        Catch ex As Exception

        End Try

        Return RetVal

    End Function

HTH Glenn

From: rickrat [mailto:notifications@codeplex.com]
Sent: Saturday, July 18, 2009 12:31 AM
To: Glenn Block
Subject: MEF 6 issue [MEF:62839]

From: rickrat

I had the following code in MEF 5

    Public Function GetExportByMetadata(Of T)(ByVal name As String) As Export(Of T)
        Dim RetVal As Export(Of T) = Nothing
 
        Try
            RetVal = _Container.GetExports(Of T).Where(Function(p) p.Metadata.ContainsKey(name) = True).Single()
        Catch ex As Exception
 
        End Try
 
        Return RetVal
    End Function
 

But when I converted it to MEF 6, I get this:

    Public Function GetExportByMetadata(Of T, U)(ByVal name As String) As Lazy(Of T, U)
        Dim RetVal As Lazy(Of T, U) = Nothing
 
        Try
            RetVal = _Container.GetExports(Of T, U).Where(Function(p) p.Metadata.ContainsKey(name) = True).Single()
        Catch ex As Exception
 
        End Try
 
        Return RetVal
    End Function
 
The problem is:
Error 36 Overload resolution failed because no accessible 'Where' can be called with these arguments:
Extension method 'Public Function Where(predicate As System.Func(Of System.Lazy(Of T, U), Integer, Boolean)) As System.Collections.Generic.IEnumerable(Of System.Lazy(Of T, U))' defined in 'System.Linq.Enumerable': Nested function does not have the same signature as delegate 'System.Func(Of System.Lazy(Of T, U), Integer, Boolean)'.
Extension method 'Public Function Where(predicate As System.Func(Of System.Lazy(Of T, U), Boolean)) As System.Collections.Generic.IEnumerable(Of System.Lazy(Of T, U))' defined in 'System.Linq.Enumerable': 'ContainsKey' is not a member of 'U'.
I don't know how to proceed to convert this to MEF 6 or how to fix the error. Please help?
 

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