clonedParms[i] = (SqlParameter) ((ICloneable) cachedParms[i]).Clone();想问下这一句为什么要强制转换成(ICloneable)接口类型再调用Clone()方法,既然cachedParms[i]能够转换成ICloneable接口,就说明它实现了ICloneable.Clone()方法,那么直接像这样cachedParms[i].Clone()调用不就好了么?同样的疑问还存在于IList<T> list = new List<T>(); 
直接定义成 List<T> list = new List<T>(); 不行么,非要用 IListprivate 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++)
//问题在这里
clonedParms[i] = (SqlParameter) ((ICloneable) cachedParms[i]).Clone(); return clonedParms;
}

解决方案 »

  1.   

    cachedParms[i]不是直接的ICloneable类型,SqlParameter没有clone方法
      

  2.   

     (ICloneable) cachedParms[i]:这个是强制类型转换,相当于从基类到派生,或者到接口,
    因为cachedParams是一个集合类,如果cachedParams的元素都实现了ICloneable接口,当然不用强制类型转换,否则要转一下。
    IList<T> list = new List<T>();  
    是用一个派生类实例化一个基类。相当于
    鸟 鸟1 = 一个乌鸦。
      

  3.   

    对于如下的情况,就必须强制转换:class MyClass : ICloneable 
    {
        object ICloneable.Clone()
        {
            return new MyClass();
        }
    }MyClass c = new MyClass();
    c.Clone(); // 报错
    ((ICloneable)c).Clone(); // 可以
      

  4.   

    这就是我上面说的,显示实现接口。
    显示实现接口是很特定的情况使用的,并且msdn上标注了不推荐写法,因为继承类并无法继承显示接口的实现。换句话说。显示接口实现是非面向对象的。
    http://msdn.microsoft.com/zh-cn/library/ms173157(v=vs.80).aspx