泛型方法

在开发CS插件时,使用了泛型集合Dictionary和List,但是时常要调用Components中的一些方法要传入ArrayList集合,但暂时不想修改核心组件,所以需要把泛型转为非泛型,使用泛型方法是一个比较好的方法。

        public static ArrayList GenericToArrayList<T>(List<T> list)
        
{
            ArrayList alist 
= new ArrayList();

            
foreach (T t in list)
            
{
                alist.Add(t);
            }


            
return alist;
        }

        public static Hashtable GenericToHashtable<TKey, TValue>(Dictionary<TKey, TValue> dic)
        
{
            Hashtable ht 
= new Hashtable();

            Dictionary
<TKey, TValue>.Enumerator enumerator = dic.GetEnumerator();
            
while (enumerator.MoveNext())
            
{
                ht.Add(enumerator.Current.Key, enumerator.Current.Value);
            }


            
return ht;
        }

附:在VS2005中设置跟踪活动项。
选项-->项目和解决方案-->在解决方案资源管理器中跟踪活动项
posted @ 2006-03-19 11:11  chenjunbiao  阅读(175)  评论(0编辑  收藏  举报