卸载Class?

由于在Java中有一个可以自己替换的ClassLoader,所以在Eclipse中很容易管理成千上万的插件,可以在同一个应用域中自由地装载和卸载。在运行时为某个Class重新定义一个ClassLoader并且重新实例化,原来加载的东西就交给GC了。这个机制还有一个我一直梦想的一个价值就是在Load一个Class到应用域的时候运行某些方法,例如读出配置文件。如果配置文件中涉及到一些额外的Class需要更替就不必重新启动应用程序了。这对于需要类似热部署的场合非常有用。
在.net中没有这么幸运。Assembly是决不能被卸载的。有一个解决方案是通过卸载AppDomain的方式来卸载一系列的Assembly。经过一小番试验,跨域的调用是可以实现的,不过需要遵循一点点小规则。
一、只有自MashalByRefObject派生的类型可以跨应用域访问。因为Calling的域使用的中Executing域中的实例通过Unwrap出来的代理类,所以其实并不是真正的“跨应用域”,而是类似Remoting的方式。
二、所有传递的数据类型必须是加上Serializable Attribute的类型。
我的应用场景是通过选项在运行时替换一个已升级的Assembly依赖。这个依赖显然不能写到主应用程序所依赖的Assembly清单中,而是通过从配置文档读出其相关的Assembly并从指定位置加载。
下面的例子是读出一个Assembly中的所有类型并将其继承关系显示到一个TreeView中。
这是个用来传递类型信息的数据类型:

ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 可在应用域间传递的详细类型信息
ExpandedBlockEnd.gif    
/// </summary>

None.gif     [Serializable]
None.gif    
public   class  TypeInfo
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public TypeInfo(Type type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _Name 
= type.Name;
InBlock.gif            _FullName 
= type.FullName;
InBlock.gif            _BaseName 
= type.BaseType.FullName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _Name, _FullName, _BaseName;
InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _Name;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string FullName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _FullName;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string BaseName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _BaseName;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
这是个装载器:
ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 跨域工作的程序集装载器
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  AssemblyLoader : MarshalByRefObject
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public TypeInfo [] GetAssemblyTypeInfos(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Assembly assembly 
= Assembly.LoadFile(path);
InBlock.gif            Type [] types 
= assembly.GetExportedTypes();
InBlock.gif            TypeInfo [] typeInfos 
= new TypeInfo[types.Length];
InBlock.gif            
int i = 0;
InBlock.gif            
foreach (Type t in types)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                typeInfos[i 
++= new TypeInfo(t);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return typeInfos;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
剩下的就是客户端代码了,拿到这个列表生成TreeView就没什么困难了:
None.gif              private  TypeInfo [] GetTypeInfos( string  path)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                AppDomain appDomain 
= AppDomain.CreateDomain("TemporaryAssembly");
InBlock.gif                Type t 
= typeof(AssemblyLoader);
InBlock.gif                AssemblyLoader loader 
= (AssemblyLoader) appDomain.CreateInstance(t.Assembly.FullName, t.FullName).Unwrap();
InBlock.gif                TypeInfo [] typeInfos 
= loader.GetAssemblyTypeInfos(path);
InBlock.gif                AppDomain.Unload(appDomain);
InBlock.gif                
return typeInfos;
ExpandedBlockEnd.gif            }

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值