【问题标题】:MEF - Change default creation policy to NonSharedMEF - 将默认创建策略更改为 NonShared
【发布时间】:2012-12-13 19:28:36
【问题描述】:

我目前正在使用 MEF 扫描/编写我的应用程序的扩展。我想将默认创建策略更改为非共享(代替共享),但要让它通过属性覆盖创建策略。换句话说,如果我不指定导出的创建策略,我希望 MEF 使用非共享。

我之前的实现不再适用于我,因为它改变了所有导入所需的创建策略。因此,如果我覆盖创建策略,则导出不会组合。

我四处搜索,最接近的是新的 RegisrationBuilder,但我正在使用 VS2010 中的 .NET 4(即 MEF 1)。我也不知道如何使用 ExportProviders。

非常感谢任何帮助;提前致谢!

【问题讨论】:

  • 我还没有尝试过 TransientCompositionContainer,但看起来它将 CreationPolicy.Any(默认)更改为 CreationPolicy.NonShared。因此,具有显式 CreationPolicy.Shared 的导入应该仍然有效,其他一切都应该是暂时的。
  • 另一种方法(尽管我从未尝试过)是创建一个自定义导入属性(这应该类似于自定义导出属性指南mef.codeplex.com/wikipage?title=Exports%20and%20Metadata),默认情况下将RequiredCreationPolicy 设置为NonShared。然后使用此属性更新所有导入。显然这不如 TransientCompositionContainer 优雅。

标签: c# dependency-injection inversion-of-control mef


【解决方案1】:

我已经成功创建了一个MEF wrapper,用作应用引导程序。 在我使用 MEF 的项目中,我有 mu 自定义属性,用于控制脚趾实例化模式。 属性的自定义实现是:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class BusinessLogicImportAttribute : ConstraintImportAttribute
{
    public BusinessLogicImportAttribute()
        : base(typeof(IBusinessController))
    {
        base.RequiredCreationPolicy = RequiredCreationPolicy = CreationPolicy.NonShared;
    }
}

【讨论】:

  • 谢谢,我试试看!
猜你喜欢
  • 2016-10-15
  • 2019-10-15
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2011-06-11
  • 1970-01-01
相关资源
最近更新 更多