初来乍到,送大家一个处理缓冲的设计
大家都是高手,我就直接送代码好了。
public interface IBufferItem
{
bool Used{ get;set;}
}
/// <summary>
/// BufferStorage 的摘要说明。
/// </summary>
public class BufferStorage
{
private static readonly string m_InterfaceName = typeof( IBufferItem ).FullName;
private System.Collections.ArrayList ar = new System.Collections.ArrayList();
public BufferStorage(){}
/// <summary>
/// 获取没有被占用的缓冲,若没有空的缓冲则自动创建一个缓冲,
/// 不可以将缓冲的Buffer在外部使用,避免缓冲不被施放导致内存无限增加
/// </summary>
/// <returns></returns>
public IBufferItem GetUnusedBuffer( System.Type type )
{
if( type.GetInterface( m_InterfaceName ) == null )
return null;
System.Threading.Monitor.Enter( ar.SyncRoot );
try
{
foreach( IBufferItem bu in ar )
{
if( ( bu.Used == false ) && ( bu.GetType() == type ) )
{
bu.Used = true;
return bu;
}
}
IBufferItem rcb = System.Activator.CreateInstance( type ) as IBufferItem;
if( rcb == null )
return null;
rcb.Used = true;
ar.Add( rcb );
return rcb;
}
catch
{
return null;
}
finally
{
System.Threading.Monitor.Exit( ar.SyncRoot );
}
}
}
浙公网安备 33010602011771号