你可以使用保湿MEF的静态属性? [英] Can you hydrate a static property using MEF?
问题描述
我可以滋润这个类的静态构造函数里?
公共类连接:IDTExtensibility2的,IDTCommandTarget
静态连接()
{
//水合物的静态属性?
}
[进口]
公共静态动作< ProjectLogicChecks>显示{获得;组; }
[导出(typeof运算(动作< ProjectLogicChecks>))]
私有静态无效DisplayResults(CheckProcesses _checkResults)
{
的MessageBox.show(_checkResults.ProjectLogicCheck.AssemblyName +有问题=+
_checkResults.ProjectLogicCheck.HasProblems);
}
这是比我想象的要容易得多。
静态连接()
{
VAR批=新CompositionBatch();
CompositionContainer中容器;
VAR reflectionCatalog =新AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
VAR extensionPath = System.IO.Path.Combine(Environment.CurrentDirectory,扩展);
如果(System.IO.Directory.Exists(extensionPath))
{
VAR directoryCatalog =新DirectoryCatalog(extensionPath);
VAR defaultCatalogEp =新CatalogExportProvider(reflectionCatalog);
集装箱=新CompositionContainer中(directoryCatalog,defaultCatalogEp);
defaultCatalogEp.SourceProvider =容器;
}
其他
集装箱=新CompositionContainer中(reflectionCatalog);
container.Compose(批);
//设置一个静态属性
显示= container.GetExportedValue<作用< IEnumerable的< ProjectLogicChecks>>>();
}
变更类型为动作< IEnumerable的< ProjectLogicChecks>>
这样我就可以显示结果为多个项目和整体解决方案,而不是只是一个
我跟着<一个href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2009/12/05/mef-for-beginner-part-2-vs-2010.aspx"相对=nofollow>这个文章,以获取静态属性集,那么<一个href="http://$c$cbetter.com/blogs/glenn.block/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx"相对=nofollow>这个,以提供本地默认的情况下,有没有扩展present。
can I hydrate this inside the class's static constructor?
public class Connect:IDTExtensibility2, IDTCommandTarget
static Connect()
{
//hydrate static properties?
}
[Import]
public static Action<ProjectLogicChecks> Display { get; set; }
[Export(typeof(Action<ProjectLogicChecks>))]
private static void DisplayResults( CheckProcesses _checkResults)
{
MessageBox.Show(_checkResults.ProjectLogicCheck.AssemblyName + " has problems=" +
_checkResults.ProjectLogicCheck.HasProblems);
}
It was easier than I thought it would be.
static Connect()
{
var batch = new CompositionBatch( );
CompositionContainer container;
var reflectionCatalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly( ));
var extensionPath = System.IO.Path.Combine(Environment.CurrentDirectory, "extensions");
if (System.IO.Directory.Exists(extensionPath))
{
var directoryCatalog = new DirectoryCatalog(extensionPath);
var defaultCatalogEp = new CatalogExportProvider(reflectionCatalog);
container=new CompositionContainer(directoryCatalog, defaultCatalogEp);
defaultCatalogEp.SourceProvider=container;
}
else
container = new CompositionContainer(reflectionCatalog);
container.Compose(batch);
//Setting a static property
Display=container.GetExportedValue<Action<IEnumerable< ProjectLogicChecks>>>( );
}
Changed the type to Action<IEnumerable<ProjectLogicChecks>>
so that I could display results for multiple projects or a whole solution instead of just the one.
I followed this article to get the static property set, then this to provide local defaults in case there is no extension present.
这篇关于你可以使用保湿MEF的静态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!