我正在尝试使用 MEF 实现工厂模式。
这是我的解决方案
核心项目
IClass
ObjectFactory static Class(This is where the problem is)
项目A
[Export(typeof(IClass))]
[ExportMetadata("Type", "TypeA")]
public classA : IClass
{}
项目B
[Export(typeof(IClass))]
[ExportMetadata("Type", "TypeB")]
public classB : IClass
{}
当我尝试动态创建对象时遇到问题
这是工厂类:
public static class ObjectFactory
{
private static readonly CompositionContainer _container;
[ImportMany]
public static IEnumerable<Lazy<IClass, IMetaData>> objectTypes;
static ObjectFactory()
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(Environment.CurrentDirectory));
_container = new CompositionContainer(catalog);
try
{
objectTypes = _container.GetExports<IClass, IMetaData>();
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
Console.ReadLine();
}
}
public static IClass CreateObject(ObectType objectType)
{
IClass outProvider;
Type typeToLoad = objectTypes.Where(x => x.Metadata.Type == objectType.ToString()).FirstOrDefault().GetType();
outProvider = (IClass)Activator.CreateInstance(typeToLoad);
return outProvider;
}
}
最佳答案
如果您希望在每次调用 CreateObject 时提供一个新的“NonShared”实例,那么我建议进行此重构。
private static readonly CompositionContainer _container;
static ObjectFactory()
{
var directoryCatalog = new DirectoryCatalog(Environment.CurrentDirectory)
_container = new CompositionContainer(directoryCatalog);
}
public static IClass CreateObject(ObectType objectType)
{
var objectTypes objectTypes = new List<Lazy<IClass, IMetaData>>();
try
{
objectTypes.AddRange(_container.GetExports<IClass, IMetaData>());
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
Console.ReadLine();
}
return objectTypes.FirstOrDefault(x => x.Metadata.Type == objectType.ToString());
}
您会看到 MEF 每次组合类型或调用 GetExports(以及此函数的所有其他重载)时都会解析新实例(即非共享实例)。或者,您可以导出 IClass 工厂,然后您将拥有这些提供程序的集合。
附注您示例中的 objectTypes 成员上的 [ImportMany] 是多余的,因为您没有编写这种类型(我不相信您甚至可以,因为它是静态的),您只需从 GetExports 的输出以编程方式设置它
关于c# - 带有托管扩展框架 (MEF) 的工厂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26292563/