调试时不调用Visual Studio Package Initialize方法

本文关键字:Package Initialize 方法 Studio Visual 调用 调试 | 更新日期: 2023-09-27 18:07:16

目前,我正在使用MEF开发Visual Studio 2010的扩展,我需要初始化我的全局状态。我试着在包装里做。初始化方法

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

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

谢谢

调试时不调用Visual Studio Package Initialize方法

我找到答案了。必须添加provideautolload属性

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