简体   繁体   English

在MEF上下文中使用泛型

[英]Using Generics in MEF-Context

I want to use generics in combination with MEF-"[ImportMany(....))]", but I get compile-errors. 我想将泛型与MEF-“ [ImportMany(....))]”结合使用,但出现编译错误。

The following code without using generics works fine : The factory-class "HelperOneFactory" (see below) is searching für all implementations of the "IHelperOne"-interface. 以下代码无需使用泛型即可正常工作 :工厂类“ HelperOneFactory”(请参见下文)正在搜索“ IHelperOne”接口的所有实现。 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): 因此,我尝试对接口类型使用泛型 ,但是随后出现编译错误(使用.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? 是否可以对“ ImportMany”命令使用通用类型?

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. “常规”类“ HelperOneOriginal”是HelperOne的标准版本,可以通过定义子类“ HelperOneCustom”将其覆盖在Custom-Projects中,该子类通常放置在单独的VisualStudio-Project中。 Both classes have the interface "IHelperOne". 这两个类都有接口“ IHelperOne”。

The main-Program should use the Custom-class if defined, or otherwise the Original-class. 主程序应使用Custom类(如果已定义),否则应使用Original类。 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". 因此,“ HelperOneFactory”将查找“ IHelperOne”-接口的所有实现,并采用不带元数据“ 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. 对存储在HelperClass中的相关类ist的引用,该成员在“ Current”成员中供主程序使用。

If necessary a small test-project can be provided. 如有必要,可以提供一个小的测试项目。

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 感谢Dave M

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM