简而言之,就是相应AppDomain.的AssemblyResolve实现的。
DynAssembly提供给动态载入的模块提供一个解析名字的机会。如下代码可以把NHibernate 的Hbm.xml 生成 数据库结构。
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace Hbm2Sql
{
class DynAssembly
{
System.Reflection.Assembly _asm;
public DynAssembly(System.Reflection.Assembly asm)
{
this._asm = asm;
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(this.TypeResolve);
}
public System.Reflection.Assembly TypeResolve(object sender, ResolveEventArgs args)
{
if (_asm.FullName.Contains(args.Name))
{
return _asm;
}
return null;
}
}
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length > 0)
{
System.Console.Write("命令行:");
System.Console.WriteLine(string.Join(" ",args));
System.Reflection.Assembly asm = AppDomain.CurrentDomain.Load(System.Reflection.AssemblyName.GetAssemblyName(args[0]));
new DynAssembly(asm);
NHibernate.Cfg.Configuration cfg = new Configuration().Configure()
.AddAssembly(asm);
SchemaExport export = new SchemaExport(cfg);
export.Create(true, false);
}
}
catch (System.Exception e)
{
System.Console.Write(e.ToString());
}
finally
{
System.ConsoleKeyInfo i = System.Console.ReadKey();
}
}
}
}
using System.Collections.Generic;
using System.Text;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace Hbm2Sql
{
class DynAssembly
{
System.Reflection.Assembly _asm;
public DynAssembly(System.Reflection.Assembly asm)
{
this._asm = asm;
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(this.TypeResolve);
}
public System.Reflection.Assembly TypeResolve(object sender, ResolveEventArgs args)
{
if (_asm.FullName.Contains(args.Name))
{
return _asm;
}
return null;
}
}
class Program
{
static void Main(string[] args)
{
try
{
if (args.Length > 0)
{
System.Console.Write("命令行:");
System.Console.WriteLine(string.Join(" ",args));
System.Reflection.Assembly asm = AppDomain.CurrentDomain.Load(System.Reflection.AssemblyName.GetAssemblyName(args[0]));
new DynAssembly(asm);
NHibernate.Cfg.Configuration cfg = new Configuration().Configure()
.AddAssembly(asm);
SchemaExport export = new SchemaExport(cfg);
export.Create(true, false);
}
}
catch (System.Exception e)
{
System.Console.Write(e.ToString());
}
finally
{
System.ConsoleKeyInfo i = System.Console.ReadKey();
}
}
}
}