读书人

petshop4中方法GetCachedParameters(s

发布时间: 2012-02-27 10:00:22 作者: rapoo

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[],作用是什么?
==========防止你调用后改变缓存中存在的参数

读书人网 >asp.net

热点推荐