[英]How to know the original type of a class Exported as an interface in MEF
是否可以使用協定名稱通過mef導出為接口導出的類的類型。
例如,我要獲取MailViewModel類型:
[Export(typeof(IPlugin), "Mail")
public class MailViewModel: IPlugin { }
使用MEF,我可以為合同名稱“ Mail”獲得一個懶惰,但我不知道如何獲取MailViewModel類型。
我需要知道這種類型,因為我可以在作為IPlugin導出的類上具有特定屬性。 根據此屬性,我將允許或不允許創建此插件的值(在導航方案中)。
當我使用[Export]以其原始類型導出類時,可以編寫此代碼以了解特定屬性是否裝飾了我的類:
Attribute.GetCustomAttributes(typeof(myviewmodel), typeof(myattribute));
了解導出的類型和合同名稱(IPlugin和“郵件”)后,如何知道導出的類是否使用特定屬性進行修飾(不實例化)。
這將為您提供具有特定類型和合同名稱的出口的所有類型。 這是我對這個博客的看法http://www.codewrecks.com/blog/index.php/2012/05/08/getting-the-list-of-type-associated-to-a-given-export-in -mef /
private static IEnumerable<Type> GetExportTypes(ComposablePartCatalog catalog, Type type, string contractName)
{
return catalog.Parts.Where(
part =>
part.ExportDefinitions.Any(
e =>
e.ContractName == contractName && e.Metadata.ContainsKey("ExportTypeIdentity") &&
e.Metadata["ExportTypeIdentity"].Equals(
type.FullName))).Select(part => ReflectionModelServices.GetPartType(part).Value);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.