日期:2014-05-18  浏览次数:20401 次

petshop4中方法GetCachedParameters(string cacheKey)如何理解?
代码如下:
namespace PetShop.DBUtility 
{
  public abstract class SqlHelper 
  {
  // Hashtable to store cached parameters
  private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
   
  public static SqlParameter[] GetCachedParameters(string cacheKey) {
  SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];

  if (cachedParms == null)
  return null;

  SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];

  for (int i = 0, j = cachedParms.Length; i < j; i++)
  [color=#FF0000]clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();

  return clonedParms;
  }
  }
}[/color]

请问:
1.为什么要使用Clone方法返回SqlParameter[],作用是什么? (代码红色部分)

------解决方案--------------------
要为其中的各个 parameter 赋值的吧,
如果两个页面请求了这个参数数组,
同时赋值就麻烦了
------解决方案--------------------
研究PETSHOP中我也出现了一大堆的问题.过来学习的~~
------解决方案--------------------
引用楼主 lion533335 的帖子:
代码如下:
namespace PetShop.DBUtility
{
public abstract class SqlHelper
{
// Hashtable to store cached parameters
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

public static SqlParameter[] GetCachedParameters(string cacheKey) {
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];


------解决方案--------------------
直接返还就是引用
克隆返还的对象--操作内存
不同的调用者给不同的值

------解决方案--------------------
1.为什么要使用Clone方法返回SqlParameter[],作用是什么?
==========防止你调用后改变缓存中存在的参数