Type.GetType 获得本Assembly以外的类型

   简而言之,就是相应AppDomain.的AssemblyResolve实现的。
   DynAssembly提供给动态载入的模块提供一个解析名字的机会。如下代码可以把NHibernate 的Hbm.xml 生成 数据库结构。

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  NHibernate.Cfg;
None.gif
using  NHibernate.Tool.hbm2ddl;
None.gif
None.gif
namespace  Hbm2Sql
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class DynAssembly
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Reflection.Assembly _asm;
InBlock.gif
InBlock.gif        
public DynAssembly(System.Reflection.Assembly asm)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._asm = asm;
InBlock.gif            AppDomain.CurrentDomain.AssemblyResolve 
+= new ResolveEventHandler(this.TypeResolve);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Reflection.Assembly TypeResolve(object sender, ResolveEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_asm.FullName.Contains(args.Name))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _asm;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (args.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    System.Console.Write(
"命令行:");
InBlock.gif                    System.Console.WriteLine(
string.Join(" ",args));
InBlock.gif
InBlock.gif                    System.Reflection.Assembly asm 
= AppDomain.CurrentDomain.Load(System.Reflection.AssemblyName.GetAssemblyName(args[0]));
InBlock.gif                    
new DynAssembly(asm);
InBlock.gif                    NHibernate.Cfg.Configuration cfg 
= new Configuration().Configure()
InBlock.gif                                                        .AddAssembly(asm);
InBlock.gif                    SchemaExport export 
= new SchemaExport(cfg);
InBlock.gif                    export.Create(
truefalse);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (System.Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Console.Write(e.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.ConsoleKeyInfo i 
= System.Console.ReadKey();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/thh/archive/2007/03/16/676640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值