C# 属性必须实现接口 变量metadataAttribute= type.GetCustomAttributes() .SelectMany( a=> a、 GetType() .GetCustomAttributes() 第()类 .Concat(新的[]{a}.OfType()) .OfType().SingleOrDefault(); //如果我们找到了正确的元数据 if(metadataAttribute!=null) { //返回懒惰的工厂 返回新的KeyValuePair(类型,metadataAttribute); } } } //来自http://www.codewrecks.com/blog/index.php/2012/05/08/getting-the-list-of-type-associated-to-a-given-export-in-mef/ 公共静态IEnumerable GetExportTypes(此CompositionContainer mefContainer) T:在哪里上课 { //查看mef目录,找出T型的所有类型 返回mefContainer.Catalog.Parts.Where(part=>part.ExportDefinitions .任何( def=> def.Metadata.ContainsKey( “ExportTypeIdentity”)&& 定义元数据[“ExportTypeIdentity”] .等于( typeof(T.FullName))) .可计算的() .Select(part=>ReflectionModelServices.GetPartType(part).Value); } }
看起来您正在尝试将其用作unity容器,最好只使用unit容器。我们目前使用MEF来查找所有的注册,这些注册是带有元数据的导出属性,元数据说明了如何注册这些属性。然后执行与上面类似的操作来填充unity容器,而不是属性 你会像这样使用它C# 属性必须实现接口 变量metadataAttribute= type.GetCustomAttributes() .SelectMany( a=> a、 GetType() .GetCustomAttributes() 第()类 .Concat(新的[]{a}.OfType()) .OfType().SingleOrDefault(); //如果我们找到了正确的元数据 if(metadataAttribute!=null) { //返回懒惰的工厂 返回新的KeyValuePair(类型,metadataAttribute); } } } //来自http://www.codewrecks.com/blog/index.php/2012/05/08/getting-the-list-of-type-associated-to-a-given-export-in-mef/ 公共静态IEnumerable GetExportTypes(此CompositionContainer mefContainer) T:在哪里上课 { //查看mef目录,找出T型的所有类型 返回mefContainer.Catalog.Parts.Where(part=>part.ExportDefinitions .任何( def=> def.Metadata.ContainsKey( “ExportTypeIdentity”)&& 定义元数据[“ExportTypeIdentity”] .等于( typeof(T.FullName))) .可计算的() .Select(part=>ReflectionModelServices.GetPartType(part).Value); } },c#,unity-container,mef,windows-phone-8.1,C#,Unity Container,Mef,Windows Phone 8.1,看起来您正在尝试将其用作unity容器,最好只使用unit容器。我们目前使用MEF来查找所有的注册,这些注册是带有元数据的导出属性,元数据说明了如何注册这些属性。然后执行与上面类似的操作来填充unity容器,而不是属性 你会像这样使用它 ViewModelsLazy = mefContainer.GetExportsResolved<ViewModelBase, IViewModelMetadata>(unityContainer); ViewModelsLazy=mefConta
ViewModelsLazy = mefContainer.GetExportsResolved<ViewModelBase, IViewModelMetadata>(unityContainer);
ViewModelsLazy=mefContainer.GetExportsResolved(unityContainer);
同样,使用unity容器并执行以下操作可能更好
var regs = mefContainer.GetExportTypesWithMetadata<ViewModelBase, IViewModelMetadata>();
foreach (var reg in regs)
{
unityContainer.RegisterType(typeof (ViewModelBase), reg.Key, reg.Value.Name);
}
var regs=mefContainer.GetExportTypesWithMetadata();
foreach(regs中的var reg)
{
unityContainer.RegisterType(typeof(ViewModelBase)、reg.Key、reg.Value.Name);
}
当您想要解析一个类时,您将转到unity并执行解析,将名称作为合同名称传入
unityContainer.Resolve<ViewModelsLazy.Single(v => v.Metadata.Name.Equals(viewModel)).Value>();
public static class MEFExtensions
{
public static IEnumerable<Lazy<T, M>> GetExportsResolved<T, M>(this CompositionContainer mefContainer,
IUnityContainer unityContainer)
where T: class where M: class
{
// wrap the resolve around unity resolve then change type to T
return mefContainer.GetExportTypesWithMetadata<T, M>()
.Select(kv => new Lazy<T, M>(() => unityContainer.Resolve(kv.Key) as T, kv.Value));
}
public static IEnumerable<KeyValuePair<Type, M>> GetExportTypesWithMetadata<T, M>(
this CompositionContainer mefcontainer)
where T : class where M : class
{
// need to examine each type to see if they have the correct export attribute and metadata
foreach (var type in mefcontainer.GetExportTypes<T>())
{
// should just be one if more than one will throw exception
// metadata or export attribute has to implement the interface
var metadataAttribute =
type.GetCustomAttributes()
.SelectMany(
a =>
a.GetType()
.GetCustomAttributes()
.OfType<MetadataAttributeAttribute>()
.Concat<Attribute>(new[] { a }.OfType<ExportAttribute>()))
.OfType<M>().SingleOrDefault();
// if we found the correct metadata
if (metadataAttribute != null)
{
// return the lazy factory
yield return new KeyValuePair<Type, M>(type, metadataAttribute);
}
}
}
//Idea from http://www.codewrecks.com/blog/index.php/2012/05/08/getting-the-list-of-type-associated-to-a-given-export-in-mef/
public static IEnumerable<Type> GetExportTypes<T>(this CompositionContainer mefContainer)
where T : class
{
// look in the mef catalog to grab out all the types that are of type T
return mefContainer.Catalog.Parts.Where(part => part.ExportDefinitions
.Any(
def =>
def.Metadata.ContainsKey(
"ExportTypeIdentity") &&
def.Metadata["ExportTypeIdentity"]
.Equals(
typeof (T).FullName)))
.AsEnumerable()
.Select(part => ReflectionModelServices.GetPartType(part).Value);
}
}
ViewModelsLazy = mefContainer.GetExportsResolved<ViewModelBase, IViewModelMetadata>(unityContainer);
var regs = mefContainer.GetExportTypesWithMetadata<ViewModelBase, IViewModelMetadata>();
foreach (var reg in regs)
{
unityContainer.RegisterType(typeof (ViewModelBase), reg.Key, reg.Value.Name);
}