Tag: mef

使用MEF时,Type.GetType返回null

我目前正在使用MEF来导入插件项目,因为插件是用WPF编写的,每个插件都有一个视图和一个viewmodel。 插件知道viewmodel,但主shell UI将构造视图并使用约定优于配置类型模式绑定viewmodel。 我使用了Build-your-own-MVVM-framework示例中的一些代码来执行自动视图发现: [ImportMany(typeof(IPlugin))] public IEnumerable Plugins { get; set; } var viewTypeName = this.Plugins.First().ViewModel.GetType().AssemblyQualifiedName.Replace(“Model”, string.Empty); var viewType = Type.GetType(viewTypeName,true); 目前的代码只是获取第一个插件并从名称中取出Model ,返回视图名称并获取视图类型以便我可以构造它。 那么viewType的一个例子是: PluginTest.TestView, PluginTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 但是,当我调用Type.GetType(viewType)我得到null,如果我添加true以抛出exception,我得到exception: Could not load file or assembly ‘PluginTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The system cannot find the file specified. 即使已经使用MEF加载了它。 如果我做: var […]

使用托管包框架实现语言服务

我已经按照步骤( http://msdn.microsoft.com/en-us/library/bb166360.aspx )中列出的步骤来实现和注册LanguageService,但我根本无法使用它。 我的包正确加载,我可以添加function菜单项和选项页,但我无法打开注册到我的服务的文件。 当我附加一个调试器时,我可以看到我的VSPackage被构造,加载,并且它的FDoIdle方法被调用了两次。 在那之后,实验性的Visual Studio会话进入杂草并且似乎陷入无限循环。 如果我尝试将.txt文件保存为我的文件类型,则文件会正确保存,但不会在Visual Studio中加载,我会收到Object nullexception。 我可以看到我在这个问题中提到的问题: Visual Studio 2010着色器,intellisense和其他人。 从哪儿开始! 解决方案似乎是不使用Managed Package Framework而是使用Manage Extensibility Framework。 这很好,但是我真的很想知道我的MPF语言服务实现有什么问题。 有没有人使用MPF成功创建了LanguageService? 任何人都可以指导我一个工作示例或演练吗? 问题解决了!: 我做了一些进一步的调试,并注意到实验性的Visual Studio实例卡住了以下重复调用以下内容: Microsoft.VisualStudio.Editor.Implementation.VsFontsAndColorsInformation.TryGetIndexForNativeItem(String name,IVsColorTable colorTable,Int32&colorTableIndex) 在预感中,我在ProvideLanguageServiceAttribute中将RequestStockColors参数指定为true,现在我可以打开我的文件类型。 我的简单扫描仪甚至还有工作语法着色!

从编辑器中捕获未处理的exception

我们在WPF应用程序(MVVM)中使用MEF来嵌入外部编辑器。 在我们的主视图中的某个时刻,有一个内容元素,其中将放置编辑器。 现在我们想从该编辑器中捕获任何未处理的exception,然后重新加载编辑器。 我发现的唯一的事情是使用Application类中的DispatcherUnhandledException 。 从那里我将不得不以某种方式到达主视图编辑器并告诉它重新加载崩溃的编辑器。 我想知道是否有一个“较低”的水平点,我可以抓住exception? 有没有人有一些经验,可以帮助他在这里? 谢谢

通过.NET Core上的MEF将参数传递给插件构造函数?

我花了几个小时试图弄清楚如何通过MEF(System.Composition)将参数传递给插件构造函数,但都无济于事。 毋庸置疑,相关文档很少,通过源代码看看也无济于事。 这曾经很容易使用CompositionHost.ComposeExportedValue方法,但在.NET Core版本中我似乎无法找到任何有用的东西。 我在下面附上了我的不完整代码,然后是抛出的exception。 在这方面的任何帮助将不胜感激。 谢谢…. using System; using System.Composition; using System.Composition.Hosting; using System.Reflection; namespace MefMe { public interface IPlugin { void Alert(); } [Export(typeof(IPlugin))] public class Plugin : IPlugin { private string code; [ImportingConstructor] public Plugin(string code) { this.code = code; } public void Alert() => Console.WriteLine(code); } class Program { static void […]

使用MEF进行线程安全延迟实例化

// Member Variable private static readonly object _syncLock = new object(); // Now inside a static method foreach (var lazyObject in plugins) { if ((string)lazyObject.Metadata[“key”] = “something”) { lock (_syncLock) { // It seems the `IsValueCreated` is not up-to-date if (!lazyObject.IsValueCreated) lazyObject.value.DoSomething(); } return lazyObject.value; } } 在这里,我需要每个循环同步访问。 有很multithreading迭代这个循环,并根据它们正在寻找的key ,创建并返回一个惰性实例。 lazyObject应该lazyObject创建lazyObject 。 虽然Lazy类是这样做的,尽管使用了锁,但在高线程下我创建了多个实例(我在一个volatile static int上使用Interlocked.Increment跟踪它并将其记录到某处)。 […]

如果没有无参数构造函数,C#/ MEF不适用于基类

我有一个Prim类,它为MEF实现了一个IPrimitiveDecomposer接口并inheritance了Node基类。 public class Node { public Node() { } } public interface IPrimitiveDecomposer { bool Match(Node node); } [Export(typeof(IPrimitiveDecomposer))] public class Prim : Node, IPrimitiveDecomposer { public bool Match(Node node) {return true;} } 但是,当我inheritance一个没有无参数构造函数的类时,MEF的ComposeParts()方法无法导入Prim对象。 我在MSDN中添加了此页面之后的ImportingConstructor属性,因为我收到了没有该属性的编译错误。 [Export(typeof(IPrimitiveDecomposer))] public class Prim : Node, IPrimitiveDecomposer { [ImportingConstructor] public Prim(int val) : base (val) {} public bool Match(Node node) […]

MEF CachedAssemblyCatalog – 延迟加载程序集

我对旧版本MEF中提供的示例中引入的CachedAssemblyCatalog类非常感兴趣 – 它允许加载仅包含导出数据而不是整个程序集的小型程序集,并且只在该程序CachedAssemblyCatalog部件加载时才加载完整程序集。需要。 我想在我的应用程序中使用此function,但我不确定此代码的稳定性/可靠性,以及是否有任何关于此function的未来计划。 是否计划很快成为MEF不可分割的一部分,或者根本不成为? 有没有人在他的应用程序中使用此代码并可以分享他的印象? 它稳定吗? 它是否按预期工作? 我对谷歌此类目录中几乎没有可用数据以及缓存样本不再出现在当前可用样本中的事实感到有点气馁。 任何输入将不胜感激。

MEF’导出不能分配给’错误’

我刚开始使用MEF并且遇到了早期问题。 我有一个名为DataService的接口: namespace DataAccess { interface IDataService { string Name { get; } string Description { get;} List GetPeople(); } } 该接口有2个实现,一个用于SQL Server,另一个用于Oracle。 下面是Oracle实现,SQL Server实现完全一样。 namespace DataAccess { [Export(typeof(IDataService))] [ExportMetadata(“Name”,”Oracle”)] [ExportMetadata(“Description”,”Oracle Data Service”)] public class Oracle : IDataService { #region IDataService Members public string Name { get { return “Oracle”; } } public string Description […]

在从XAML实例化的控件中使用MEF

我有一个UserControl我创建了使用[Import]属性[Import]几个部分。 public class MyUserControl : UserControl, IPartImportsSatisfiedNotification { [Import] public IService Service { get; set; } public MyUserControl() { } public void OnImportsSatisfied() { // Do something with Service. } } 此UserControl是从XAML实例化的,因此不满足其导入,并且未调用OnImportsSatisfied 。 我的问题是如何在XAML中创建类时满足我的类的导入。

为什么我无法调试动态加载的程序集?

我正在开发一个Web API项目,该项目使用内部模拟框架,允许拦截和修改来自控制器的响应。 它使用MEF加载一个程序集,该程序集包含在匹配某些前提条件时执行的代码。 我知道这是正常的,因为我可以在响应中看到模拟已经执行,但由于某种原因我无法调试动态加载的程序集中的代码。 虽然断点看起来很活跃,但执行永远不会破坏。 我试过调用Debugger.Break(); 它确实中断,但调用堆栈显示为空,Visual Studio仅显示此消息: 我可以看到程序集及其符号已加载到模块窗口中: 我可以在调用动态加载的程序集( behavior参数)之前中断,如下所示: private HttpResponseMessage ApplyBehavior( IMockBehavior behavior, string controller, string action, HttpRequestMessage request, HttpResponseMessage mockedResponse) { return behavior.Apply(controller, action, request, mockedResponse); } 如果我尝试在立即窗口中检查behavior变量,Visual Studio将显示以下exception: behavior.GetType() ‘behavior.GetType()’ threw an exception of type ‘System.IO.FileNotFoundException’ Data: {System.Collections.ListDictionaryInternal} FileName: null FusionLog: null HResult: -2147024894 HelpLink: null InnerException: null Message: “Cannot […]