调试时不调用 Visual Studio 包初始化方法

发布于 2024-12-02 07:15:09 字数 1452 浏览 2 评论 0原文

目前,我正在使用 MEF 开发 Visual Studio 2010 的扩展,并且需要初始化我的全局状态。我正在尝试在 Package.Initialize 方法中执行此操作

[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0.0.0", IconResourceID = 400)]
[Guid("1AF4B41B-F2DF-4F49-965A-816A103ADFEF")]
public sealed class MyPackage : Package
{

    protected override void Initialize()
    {
        ContainerConfigurator.Configure();
        ContainerConfigurator.IsInitialized = true;
        base.Initialize();
    }
}

另外,我有一个使用此状态的 MEF 分类器提供

[Export(typeof(IClassifierProvider))]
[Name("This is my provider")]
[ContentType("DebugOutput")]
[ContentType("Output")]
public class MyClassifierProvider : IClassifierProvider
{
    [Import]
    private IClassificationTypeRegistryService _classificationRegistry = null; // MEF

    public IClassifier GetClassifier(ITextBuffer textBuffer)
    {
        // This always false
        if (!ContainerConfigurator.IsInitialized)
           throw new InvalidOperationException();

        return textBuffer.Properties.GetOrCreateSingletonProperty(() => new TypedClassifier(ServiceLocator.Current, _classificationRegistry));
    }
}

程序 包和 MEF 分类器都在同一个程序集中。当我开始调试并放置断点时,我看到该程序集已加载。但 MyClassifierProvider 已在 MyPackage.Initialize 调用之前初始化。因此,在启动任何 MEF 组件之前,我无法初始化全局状态。谁能解释为什么以及如何避免这种行为?

谢谢

Currently, I'm developing an extension to Visual Studio 2010 using MEF and I need to initialize my global state. I'm trying to do it in Package.Initialize method

[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0.0.0", IconResourceID = 400)]
[Guid("1AF4B41B-F2DF-4F49-965A-816A103ADFEF")]
public sealed class MyPackage : Package
{

    protected override void Initialize()
    {
        ContainerConfigurator.Configure();
        ContainerConfigurator.IsInitialized = true;
        base.Initialize();
    }
}

Also I have a MEF classifier provider that uses this state

[Export(typeof(IClassifierProvider))]
[Name("This is my provider")]
[ContentType("DebugOutput")]
[ContentType("Output")]
public class MyClassifierProvider : IClassifierProvider
{
    [Import]
    private IClassificationTypeRegistryService _classificationRegistry = null; // MEF

    public IClassifier GetClassifier(ITextBuffer textBuffer)
    {
        // This always false
        if (!ContainerConfigurator.IsInitialized)
           throw new InvalidOperationException();

        return textBuffer.Properties.GetOrCreateSingletonProperty(() => new TypedClassifier(ServiceLocator.Current, _classificationRegistry));
    }
}

Both of package and MEF classifier are in the same assembly. When I start debugging and place a breakpoint I see that this assemly is loaded. But MyClassifierProvider has been initialized before MyPackage.Initialize call. So I can't initialize my global state before any of MEF components is started. Can anyone explain why and how can I avoid that behavior?

Thanks

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

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

发布评论

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

评论(1

坏尐絯 2024-12-09 07:15:09

我已经找到答案了。需要添加 ProvideAutoLoad 属性

http://msdn。 microsoft.com/en-us/library/microsoft.visualstudio.vsconstants(v=vs.80).aspx

http://dotneteers.net/blogs/divedeeper/archive/2008/03/23/LVNSideBar1。 aspx

所以最终的类声明是

[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0.0.0", IconResourceID = 400)]
[Guid("1AF4B41B-F2DF-4F49-965A-816A103ADFEF")]
[ProvideAutoLoad("ADFC4E64-0397-11D1-9F4E-00A0C911004F")]
public sealed class MyPackage : Package

I've found the answer. It is necessary to add ProvideAutoLoad attribute

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants(v=vs.80).aspx

http://dotneteers.net/blogs/divedeeper/archive/2008/03/23/LVNSideBar1.aspx

so the final class declaration is

[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0.0.0", IconResourceID = 400)]
[Guid("1AF4B41B-F2DF-4F49-965A-816A103ADFEF")]
[ProvideAutoLoad("ADFC4E64-0397-11D1-9F4E-00A0C911004F")]
public sealed class MyPackage : Package
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文