即使解决方案如此明显,我也不应该发布此内容,但我将其留作提醒和对他人有用的参考点。
我的 app.config 文件中有以下内容:
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
其次是:
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object name="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
</objects>
</spring>
然后在我的应用程序中,我得到了:
using Spring.Context;
using Spring.Context.Support;
public partial class AlbumChecker : Window
{
private DataTable dataTable;
private Library library;
private Thread libraryThread;
public AlbumChecker()
{
InitializeComponent();
CreateToolTips();
IApplicationContext ctx = ContextRegistry.GetContext();
library = (Library)ctx.GetObject("mediaLibrary");
// Other initialisation
}
// Other code
}
这一切都编译得很好,但是,我在调用 GetContext() 时遇到了一个异常:
Error creating context 'spring.root': Could not load type from string value
'AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF'.
我检查了 Spring.NET 文档,看不出我做错了什么——但我显然有问题,否则它不会引发异常!
AlbumLibraryWPF
是命名空间,AlbumLibraryWPF.AlbumLibrary
是我要实例化的类的完全限定名。我猜这是我错了,但看不出怎么做。