配置文件如下:
因为试着通过反射名称进行对象的重组没成功,结果加上个 dll属性说明类库名,然后再读取dll属性值,便可以成功通过反射获取该类型了,必须结合下面提供的类进行使用。
<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
<!--默认ActiveRecord配置-->
<config>
<add value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" key="proxyfactory.factory_class" />
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=数据库;Integrated Security=FALSE;User ID=sa;Password=123" />
</config>
<!--模块一-->
<config type="CNITSEC.Service.Entity.BaseServiceEntity" dll="CNITSEC.Service.dll">
<add value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" key="proxyfactory.factory_class" />
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=数据库;Integrated Security=FALSE;User ID=sa;Password=123" />
</config>
<!--模块二-->
<config type="CNITSEC.Exam.Entity.BaseExamEntity" dll="CNITSEC.Exam.dll">
<add value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" key="proxyfactory.factory_class" />
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=数据库;Integrated Security=FALSE;User ID=sa;Password=123" />
</config>
<!--模块三-->
<config type="CNITSEC.StaffTraining.Entity.BaseStaffTrainingEntity" dll="CNITSEC.StaffTraining.dll">
<add value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" key="proxyfactory.factory_class" />
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=数据库;Integrated Security=FALSE;User ID=sa;Password=123" />
</config>
</activerecord>
重写XmlConfigurationSource类:
解释在在注释中。
写的不好,但是没时间改(先顶着用)。
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using Castle.ActiveRecord.Framework.Config;
using Castle.ActiveRecord;
using System.Reflection;
/// <summary>
///XmlConfigurationSource 的摘要说明
/// </summary>
public class XmlConfigurationSource : InPlaceConfigurationSource
{
public XmlConfigurationSource(String xmlFileName)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFileName);
PopulateSource(doc.DocumentElement);
}
public XmlConfigurationSource(Stream stream)
{
XmlDocument doc = new XmlDocument();
doc.Load(stream);
PopulateSource(doc.DocumentElement);
}
public XmlConfigurationSource(TextReader reader)
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
PopulateSource(doc.DocumentElement);
}
protected XmlConfigurationSource()
{
}
protected void PopulateSource(XmlNode section)
{
const string Config_Node_Name = "config";
foreach (XmlNode node in section.ChildNodes)
{
if (node.NodeType != XmlNodeType.Element) continue;
if (!Config_Node_Name.Equals(node.Name))
{
String message = String.Format("Unexpected node. Expect '{0}' found '{1}'", Config_Node_Name, node.Name);
throw new ConfigurationException(message);
}
Type targetType = typeof(ActiveRecordBase);
if (node.Attributes.Count != 0)
{
XmlAttribute typeNameAtt = node.Attributes["type"];
XmlAttribute typeDLL = node.Attributes["dll"];
if (typeNameAtt == null)
{
String message = String.Format("Invalid attribute at node '{0}'. " +
"The only supported attribute is 'type'", Config_Node_Name);
throw new ConfigurationException(message);
}
if (null == typeDLL)
{
string message = string.Format("请在配置节点'{0}'中配置所引用程序集的dll名称,否则无法找到该类型", Config_Node_Name);
throw new ConfigurationException(message);
}
String typeName = typeNameAtt.Value;
String dllName = typeDLL.Value;
//由于Type.GetType是非强类型的。Type.GetType的参数是一个string,
//当string表示的目标类型不在当前程序集中,
//则运行时Type.GetType会返回null。
//解决的办法是:首先加载目标程序集,然后再使用Assembly.GetType方法。
dllName = System.Web.HttpContext.Current.Server.MapPath("~/Bin/" + dllName);
Assembly asmb = Assembly.LoadFrom(dllName);
targetType = asmb.GetType(typeName);
if (targetType == null)
{
String message = String.Format("Could not obtain type from name '{0}'", typeName);
throw new ConfigurationException(message);
}
}
Add(targetType, BuildProperties(node));
}
}
protected IDictionary<string,string> BuildProperties(XmlNode node)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (XmlNode addNode in node.SelectNodes("add"))
{
XmlAttribute keyAtt = addNode.Attributes["key"];
XmlAttribute valueAtt = addNode.Attributes["value"];
if (keyAtt == null || valueAtt == null)
{
String message = String.Format("For each 'add' element you must specify 'key' and 'value' attributes");
throw new ConfigurationException(message);
}
dict.Add(keyAtt.Value, valueAtt.Value);
}
return dict;
}
}