您可以使用 MEF 水合静态属性吗?

发布于 2024-09-14 09:47:30 字数 540 浏览 8 评论 0 原文

我可以在类的静态构造函数中水合它吗?

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神回复 2024-09-21 09:47:30

不,MEF 不支持静态导入。

No, MEF doesn't support static imports.

皇甫轩 2024-09-21 09:47:30

您可以使用 [ImportingConstructor] 并在构造函数中设置静态属性。

private static RandomService Random { get; set; }  
[ImportingConstructor] 
public ClientViewModel(RandomService random)
 {   
 Random = random; 
} 

只是不要将其设置为静态字段。

You can use [ImportingConstructor] and set the static property in the constructor.

private static RandomService Random { get; set; }  
[ImportingConstructor] 
public ClientViewModel(RandomService random)
 {   
 Random = random; 
} 

Just don't set it to a static field.

堇年纸鸢 2024-09-21 09:47:30

这比我想象的要容易。

 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>>>( );
    }

将类型更改为 Action> ,以便我可以显示多个项目或整个解决方案的结果,而不仅仅是一个。

我遵循 这篇文章来获取静态属性集,然后 提供本地默认值,以防不存在扩展名。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文