[英]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.