I'm writing a plugin for an application (Autodesk Revit Architecture 2011, if you need to know).
我正在为应用程序编写插件(如果您需要了解,可以使用Autodesk Revit Architecture 2011)。
During debugging, I'd like to recompile my plugin and reload that in the host. My specific host even provides an Add-In Manager that simplifies this process, using Assembly.Load
. For Windows.Forms plugins, this works like a charm.
在调试过程中,我想重新编译我的插件并在主机中重新加载。我的特定主机甚至提供了一个外接程序管理器,它使用Assembly.Load简化了此过程。对于Windows.Forms插件,这就像一个魅力。
When I use WPF, this breaks down. At first, I was getting an error along these lines (I added some formatting to make it easier for you to read:
当我使用WPF时,这会崩溃。起初,我在这些方面遇到错误(我添加了一些格式以便于您阅读:
System.Windows.Markup.XamlParseException:
[A]MyApp.Controls.MyControl cannot be cast to [B]MyApp.Controls.MyControl.
Type A originates from 'MyApp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '%PATHA%'.
Type B originates from 'MyApp, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '%PATHB'.
Error at object 'MyApp.Controls.MyControl' in markup file 'MyApp;component/controls/mydialog.xaml'.
I have also taken the liberty of renaming controls, namespaces and paths to protect the innocent and me, the culprit.
我也冒昧地重命名控件,命名空间和路径来保护无辜者和我,罪魁祸首。
I assume this is because the XAML parser keeps a cache of types it has already loaded.
我假设这是因为XAML解析器保留了已加载的类型的缓存。
My first step was to change the assembly versions, by setting AssemblyInfo.cs/[assembly: AssemblyVersion("2.0.*")
. This just moves the error a step further:
我的第一步是通过设置AssemblyInfo.cs / [assembly:AssemblyVersion(“2.0。*”)来更改程序集版本。这只会使错误更进一步:
System.Windows.Markup.XamlParseException:
Unable to cast object of type 'MyApp.Controls.MyControl' to type 'MyApp.Controls.MyControl'.
Error at object 'MyApp.Controls.MyControl' in markup file 'MyApp;component/controls/mydialog.xaml'.
---> System.InvalidCastException: Unable to cast object of type 'MyApp.Controls.MyControl' to type 'MyApp.Controls.MyControl'.
2 个解决方案
#1
3
It sounds to me like you'd be best off performing compilation in a separate AppDomain
. You can then throw that AppDomain
away once the compilation is done.
听起来像你最好在单独的AppDomain中执行编译。然后,您可以在编译完成后将该AppDomain抛弃。
The exact interface between your main AppDomain
and compilation AppDomain
s is not really something I can comment on since you've not provided details in your question.
由于您未在问题中提供详细信息,因此您的主AppDomain和编译AppDomains之间的确切界面并不是我可以评论的内容。
#2
2
Don't know how to clean the cache, but as workaround I'd try to load XAML directly with XamlReader.Load and see if it works. Check http://msdn.microsoft.com/en-us/library/ms590388.aspx#Y309
不知道如何清理缓存,但作为解决方法,我尝试直接使用XamlReader.Load加载XAML,看看它是否有效。查看http://msdn.microsoft.com/en-us/library/ms590388.aspx#Y309
#1
3
It sounds to me like you'd be best off performing compilation in a separate AppDomain
. You can then throw that AppDomain
away once the compilation is done.
听起来像你最好在单独的AppDomain中执行编译。然后,您可以在编译完成后将该AppDomain抛弃。
The exact interface between your main AppDomain
and compilation AppDomain
s is not really something I can comment on since you've not provided details in your question.
由于您未在问题中提供详细信息,因此您的主AppDomain和编译AppDomains之间的确切界面并不是我可以评论的内容。
#2
2
Don't know how to clean the cache, but as workaround I'd try to load XAML directly with XamlReader.Load and see if it works. Check http://msdn.microsoft.com/en-us/library/ms590388.aspx#Y309
不知道如何清理缓存,但作为解决方法,我尝试直接使用XamlReader.Load加载XAML,看看它是否有效。查看http://msdn.microsoft.com/en-us/library/ms590388.aspx#Y309