在示例 《Castle ActiveRecord 小试 -单表 CRUD》  中,测试行为 CreateBlogTest() 在实施 ActiveRecord 配置时使用了内嵌的配置文件(嵌入资源)。

_TestUnitCase\BlogTest.cs 内容:

Castle ActiveRecord 使用内嵌的配置文件using System;
Castle ActiveRecord 使用内嵌的配置文件
using NUnit.Framework;
Castle ActiveRecord 使用内嵌的配置文件
Castle ActiveRecord 使用内嵌的配置文件
using test;
Castle ActiveRecord 使用内嵌的配置文件
Castle ActiveRecord 使用内嵌的配置文件
using System.Reflection;
Castle ActiveRecord 使用内嵌的配置文件
Castle ActiveRecord 使用内嵌的配置文件
using Castle.ActiveRecord.Framework.Config;
Castle ActiveRecord 使用内嵌的配置文件
using Castle.ActiveRecord;
Castle ActiveRecord 使用内嵌的配置文件
Castle ActiveRecord 使用内嵌的配置文件
namespace test._TestUnitCase

使用嵌入资源,需要引用用于反射的 System.Reflection 命名空间,该命名空间包含了一个名为 Assembly 的类。可以使用 Assembly 的方法 GetManifestResourceStream 获得内嵌资源,但是这个方法并不是静态的,所以要先取得 Assembly 的具体实例(程序集)。

可以通过 typeof(Namespace.ClassName).Assembly 方法获得类 Namespace.ClassName 所属的程序集。因此,如果要把 ActiveRecord 的 XML 配置文件作为内嵌资源使用的话,该 XML 配置文件所在的 Namespace 必须至少包括一个类(class)。

相关文章: