如何在MEF中導出一個類型,就好像導出屬性已經應用到該類型一樣?

[英]How to export a type in MEF as if the Export Attribute had been applied to that type?


I would like to dynamically apply the MEF Export attribute to a type at run-time, exactly as if the type had had an Export attribute applied at compile time.

我希望在運行時動態地將MEF Export屬性應用到一個類型,就像該類型在編譯時應用了Export屬性一樣。

Is there a simple way to do this?

有沒有一種簡單的方法?

Barring that, is there a complex way to do this?

除此之外,還有什么復雜的方法嗎?

2 个解决方案

#1


7  

If you can afford to use .NET 4.5 (which means dropping windows XP support), you can now use MEF's attribute-less registration aka Convention Model.

如果您可以使用。net 4.5(這意味着放棄windows XP支持),那么現在您可以使用MEF的無屬性注冊,即約定模型。

In .NET4 or earlier MEF preview releases this is not supported out of the box, but MEF can still be extended by creating your own implementations of ExportProvider or ComposablePartCatalog.

在. net4或更早的MEF預覽版本中,這不是支持的,但是MEF仍然可以通過創建您自己的ExportProvider或ComposablePartCatalog實現來擴展。

The MEF Contrib Fluent Definition Provider is such an implementation which allows you to register imports and exports by method calls.

MEF設計流暢定義提供程序就是這樣一種實現,允許您通過方法調用注冊導入和導出。

The MEF Contrib Configurable Definition Provider is another which allows you to set up the imports and exports in an XML file.

MEF設計的可配置定義提供程序是另一個允許您在XML文件中設置導入和導出。

Yet another option is to do the registration with Autofac and then use its MEF integration to make the autofac components available to MEF.

另一種選擇是使用Autofac進行注冊,然后使用它的MEF集成使Autofac組件對MEF可用。

#2


1  

I'm not 100% sure but I don't think that's possible to do with MEF. One pattern to use to provide similar behavior though is the factory / provider pattern.

我不是百分之百確定,但我不認為這可能與我的基金有關。一個用於提供類似行為的模式是factory / provider模式。

interface IData {} 

interface IDataProvider {
  IData Data { get; set; }
}

[Export(IDataProvider)]
class DataProvider : IDataProvider {
  public IData { get; set; }
}

You can use this pattern to dynamically update the implementation of IData to the value you would like to use.

您可以使用此模式將IData的實現動態更新為您希望使用的值。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/11/17/720b4e06c1bdae6589a311b5d6214933.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com