本文介绍了是MEF出口缓存或者发现在要求每一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一类MyClass的,注册

[导出(typeof运算(MYCLASS))] 属性,以及

[PartCreationPolicy(CreationPolicy.Shared)]

[PartCreationPolicy(CreationPolicy.NonShared)]

和后来试图调用

compositionContainer.GetExportedValue< MYCLASS>()多次

问:与第一个电话,我将通过MEF得到我的注册类 - llokup所有已注册的组件,然后试图找到一个注册的合同。问题是关于第二次等等 - 将MEF再做全局查找或缓存某处内部

解决方案

是的,MEF perfoms一些缓存,并广泛使用延迟初始化,如果你的问题是关于MEF的表现:

1)元数据(组合的部分,定义导出和导入定义)被缓存。例如:

 公共覆盖的IEnumerable< ExportDefinition> ExportDefinitions
{
    得到
    {
        如果(this._exports == NULL)
        {
            。ExportDefinition []出口= this._creationInfo.GetExports()的ToArray< ExportDefinition>();
            锁(this._lock)
            {
                如果(this._exports == NULL)
                {
                    this._exports =出口;
                }
            }
        }
        返回this._exports;
    }
}
 

2)输出的的被缓存过:

 公共对象值
{
    得到
    {
        如果(this._exportedValue == Export._EmptyValue)
        {
            对象exportedValueCore = this.GetExportedValueCore();
            Interlocked.CompareExchange(REF this._exportedValue,exportedValueCore,Export._EmptyValue);
        }
        返回this._exportedValue;
    }
}
 

当然,使用 CreationPolicy.NonShared ,出口值变为一次又一次创建的时候,当你要求它。但是,即使在这种情况下,全局查找不进行,因为元数据被反正缓存

If I have one type MyClass, register with

[Export(typeof(Myclass))] attribute, and

[PartCreationPolicy(CreationPolicy.Shared)]

or

[PartCreationPolicy(CreationPolicy.NonShared)]

and later trying to call

compositionContainer.GetExportedValue<Myclass>() multiple times.

Question: with the first call, I will get my registered class via MEF - llokup all registered assemblies, then trying to find one registered contract. Question is about second time and so on - will MEF do global lookup again or it caches somewhere internally?

解决方案

Yes, MEF perfoms some caching and widely uses lazy initialization, if you question is about MEF performance:

1) metadata (composable parts, export definitions and import definitions) is cached. Example:

public override IEnumerable<ExportDefinition> ExportDefinitions
{
    get
    {
        if (this._exports == null)
        {
            ExportDefinition[] exports = this._creationInfo.GetExports().ToArray<ExportDefinition>();
            lock (this._lock)
            {
                if (this._exports == null)
                {
                    this._exports = exports;
                }
            }
        }
        return this._exports;
    }
}

2) exported values are cached too:

public object Value
{
    get
    {
        if (this._exportedValue == Export._EmptyValue)
        {
            object exportedValueCore = this.GetExportedValueCore();
            Interlocked.CompareExchange(ref this._exportedValue, exportedValueCore, Export._EmptyValue);
        }
        return this._exportedValue;
    }
}

Of course, when using CreationPolicy.NonShared, exported value becomes created again and again, when you requesting it. But even in this case "global lookup" isn't performed, because metadata is cached anyway.

这篇关于是MEF出口缓存或者发现在要求每一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 04:24