调试时不调用 Visual Studio 包初始化方法
目前,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经找到答案了。需要添加 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
所以最终的类声明是
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