Search code examples
c#genericsmef

Using Generics in MEF-Context


I want to use generics in combination with MEF-"[ImportMany(....))]", but I get compile-errors.

The following code without using generics works fine: The factory-class "HelperOneFactory" (see below) is searching für all implementations of the "IHelperOne"-interface. From this list it takes the first one without Metadata-value "Original". If it doesn't exist, it takes the first without checking the Metadata-value.

/// =====================================================================================
/// factory-implementation for interface-type "IHelperOne"  (==> works fine)
/// =====================================================================================
[Export(typeof(HelperOneFactory))]
public class HelperOneFactory: IPartImportsSatisfiedNotification
{
    [ImportMany(typeof(IHelperOne))]
    private IEnumerable<Lazy<IHelperOne, Dictionary<string, object>>> Helper;

    /// <summary>
    /// reference to the relevant implementaion (HelperOneOriginal or HelperOneCusto)
    /// </summary>
    public IHelperOne Current { get; private set; }

    /// <summary>
    /// looking for all implementations of IHelperOne to find out the one to use
    /// </summary>
    public void OnImportsSatisfied()
    {
        Current = Helper.Count() > 1 ? Helper.First<Lazy<IHelperOne, Dictionary<string, object>>>(s => !s.Metadata.ContainsValue("Original")).Value :
            Helper.First<Lazy<IHelperOne, Dictionary<string, object>>>().Value;
    }

That works fine, but I have to implement factory-classes for many interface-types. So I try to use generics for the interface-type, but then I get compile-errors (using .NET Framework 4.6.1):

/// =====================================================================================
/// a version with using generic ==>  compiler-errors !!
/// =====================================================================================
[Export(typeof(HelperTemplateFactory<>))]
public class HelperTemplateFactory<THelperInterface> : IPartImportsSatisfiedNotification
{
    [ImportMany(typeof(THelperInterface))]            // ==> ERROR:       "Attribute argument cannot use type parameters"
    [ImportMany(THelperInterface)]                    // ==> also ERROR:  "Type parameter name is not valid at this point"
    private IEnumerable<Lazy<THelperInterface, Dictionary<string, object>>> Helper;

    ...

Is it possible to use a generic type for the "ImportMany" command?

The context of the problem:

A "normal" class "HelperOneOriginal" is the Standard-version of the HelperOne and can be overwritten in Custom-Projects by defining a sub-class "HelperOneCustom", normaly placed in a separate VisualStudio-Project. Both classes have the interface "IHelperOne".

The main-Program should use the Custom-class if defined, or otherwise the Original-class. The Original-class has the Metadata-Information "Orginal". Therefore the "HelperOneFactory" looks for all realisations of "IHelperOne"-Interfaces and takes the first one without Metadata "Original". If it doesn't exist it takes the Original-class. The reference to the relevant class ist stored inside the HelperClass in the member "Current" for using by the main-program.

If necessary a small test-project can be provided.


Solution

  • I suggest, I have to write an "answer" to mark the question als "resolved" =>

    a line with only    "[ImportMany]"   is the solution!
    

    Thanks to Dave M