using System; using System.Collections; namespace TestApp { class Test { [STAThread] static void Main(string[] args) { ArrayList list = new ArrayList(); list.Add(3); list.Add(4); //list.Add(5.0); int total = 0; foreach(int val in list) { total = total + val; } Console.WriteLine("Total is {0}", total); } } } |
list.Add(5.0); |
Unhandled Exception: System.InvalidCastException: Specified cast is not valid. AtTestApp.Test.Main(String[]args)in :workareatestappclass1.cs:line 17 |
List<int> aList = new List<int>(); aList.Add(3); aList.Add(4); // aList.Add(5.0); int total = 0; foreach(int val in aList) { total = total + val; } Console.WriteLine("Total is {0}", total); |
//MyList.cs #region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace CLRSupportExample { public class MyList<T> { private static int objCount = 0; public MyList() {objCount++; } public int Count { get {return objCount; } } } } //Program.cs #region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace CLRSupportExample { class SampleClass {} class Program { static void Main(string[] args) { MyList<int> myIntList = new MyList<int>(); MyList<int> myIntList2 = new MyList<int>(); MyList<double> myDoubleList = new MyList<double>(); MyList<SampleClass> mySampleList = new MyList<SampleClass>(); Console.WriteLine(myIntList.Count); Console.WriteLine(myIntList2.Count); Console.WriteLine(myDoubleList.Count); Console.WriteLine(mySampleList.Count); Console.WriteLine(new MyList<sampleclass>().Count); Console.ReadLine(); } } } |
2 2 1 1 2 |
图 1.例3的MSIL |
public class Program { public static void Copy<T>(List<T> source, List<T> destination) { foreach (T obj in source) { destination.Add(obj); } } static void Main(string[] args) { List<int> lst1 = new List<int>(); lst1.Add(2); lst1.Add(4); List<int> lst2 = new List<int>(); Copy(lst1, lst2); Console.WriteLine(lst2.Count); } } |
Copy()方法就是一个泛型方法,它与参数化的类型T一起工作。当在Main()中激活Copy()时,编译器根据提供给Copy()方法的参数确定出要使用的具体类型。
7. 无限制的类型参数
如果你创建一个泛型数据结构或类,就象例3中的MyList,注意其中并没有约束你该使用什么类型来建立参数化类型。然而,这带来一些限制。如,你不能在参数化类型的实例中使用象==,!=或<等运算符,如:
if (obj1 == obj2) … |
public static T Max<T>(T op1, T op2) { if (op1.CompareTo(op2) < 0) return op1; return op2; } |
Error 1 ’T’ does not contain a definition for ’CompareTo’ |
public static T Max<T>(T op1, T op2) where T : IComparable { if (op1.CompareTo(op2) < 0) return op1; return op2; } |
public class MyClass2<T> : MyClass1<int> |
public class MyClass2<T> : MyClass2<T> |
public class MyClass2<T> : MyClass2<Y> |
public class MyClass : MyClass1<int> |
public class MyClass : MyClass1<T> |
public void Package(Basket<Fruit> aBasket) { aBasket.Add(new Apple()); aBasket.Add(new Banana()); } |
Basket<Apple> anAppleBasket = new Basket<Apple>(); Package(anAppleBasket); |
Error 2 Argument ’1’: cannot convert from ’TestApp.Basket<testapp.apple>’ to ’TestApp.Basket<testapp.fruit>’ |
public void Eat(Basket<Fruit> fruits) { foreach (Fruit aFruit in fruits) { //将吃水果的代码 } } |
Basket<Fruit> fruitsBasket = new Basket<Fruit>(); … //添加到Basket对象中的对象Fruit anAnimal.Eat(fruitsBasket); |
Basket<Banana> bananaBasket = new Basket<Banana>(); //… anAnimal.Eat(bananaBasket); |
public void Eat<t>(Basket<t> fruits) where T : Fruit { foreach (Fruit aFruit in fruits) { //将吃水果的代码 } } |
11. 泛型和代理
代理也可以是泛型化的。这样就带来了巨大的灵活性。
假定我们对写 一个框架程序很感兴趣。我们需要提供一种机制给事件源以使之可以与对该事件感兴趣的对象进行通讯。我们的框架可能无法控制事件是什么。你可能在处理某种股 票价格变化(double price),而我可能在处理水壶中的温度变化(temperature value),这里Temperature可以是一种具有值、单位、门槛值等信息的对象。那么,怎样为这些事件定义一接口呢?
让我们通过pre-generic代理技术细致地分析一下如何实现这些:
public delegate void NotifyDelegate(Object info); public interface ISource { event NotifyDelegate NotifyActivity; } |
public class StockPriceSource : ISource { public event NotifyDelegate NotifyActivity; //… } public class BoilerSource : ISource { public event NotifyDelegate NotifyActivity; //… } |
StockPriceSource stockSource = new StockPriceSource(); stockSource.NotifyActivity += new NotifyDelegate(stockSource_NotifyActivity); //这里不必要出现在同一个程序中 BoilerSource boilerSource = new BoilerSource(); boilerSource.NotifyActivity += new NotifyDelegate(boilerSource_NotifyActivity); 在代理处理器方法中,我们要做下面一些事情: 对于股票事件处理器,我们有: void stockSource_NotifyActivity(object info) { double price = (double)info; //在使用前downcast需要的类型 } |
void boilerSource_NotifyActivity(object info) { Temperature value = info as Temperature; //在使用前downcast需要的类型 } |
public delegate void NotifyDelegate<t>(T info); public interface ISource<t> { event NotifyDelegate<t> NotifyActivity; } |
public class StockPriceSource : ISource<double> { public event NotifyDelegate<double> NotifyActivity; //… } |
public class BoilerSource : ISource<temperature> { public event NotifyDelegate<temperature> NotifyActivity; //… } |
StockPriceSource stockSource = new StockPriceSource(); stockSource.NotifyActivity += new NotifyDelegate<double>(stockSource_NotifyActivity); //这里不必要出现在同一个程序中 BoilerSource boilerSource = new BoilerSource(); boilerSource.NotifyActivity += new NotifyDelegate<temperature>(boilerSource_NotifyActivity); |
void stockSource_NotifyActivity(double info) { //… } |
void boilerSource_NotifyActivity(Temperature info) { //… } |
public class MyClass<t> { } class Program { static void Main(string[] args) { MyClass<int> obj1 = new MyClass<int>(); MyClass<double> obj2 = new MyClass<double>(); Type type1 = obj1.GetType(); Type type2 = obj2.GetType(); Console.WriteLine("obj1’s Type"); Console.WriteLine(type1.FullName); Console.WriteLine(type1.GetGenericTypeDefinition().FullName); Console.WriteLine("obj2’s Type"); Console.WriteLine(type2.FullName); Console.WriteLine(type2.GetGenericTypeDefinition().FullName); } } |
obj1’s Type TestApp.MyClass`1 [[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] TestApp.MyClass`1 obj2’s Type TestApp.MyClass`1 [[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] TestApp.MyClass`1 |
List<Apple> appleList1 = new List<Apple>(); List<Apple> appleList2 = new List<Apple>(); … Copy(appleList1, appleList2); |
List<Apple> appleList1 = new List<Apple>(); List<Fruit> fruitsList2 = new List<Fruit>(); … Copy(appleList1, fruitsList2); |
Error 1 The type arguments for method ’TestApp.Program.Copy<t>(System.Collections.Generic.List<t>, System.Collections.Generic.List<t>)’ cannot be inferred from the usage. |
public static void Copy<T, E>(List<t> source, List<e> destination) where T : E |
public class MyList<t> { public void CopyTo(MyList<t> destination) { //… } } |
MyList<apple> appleList = new MyList<apple>(); MyList<apple> appleList2 = new MyList<apple>(); //… appleList.CopyTo(appleList2); |
MyList<apple> appleList = new MyList<apple>(); MyList<fruit> fruitList2 = new MyList<fruit>(); //… appleList.CopyTo(fruitList2); |
public void CopyTo<e>(MyList<e> destination) where T : E |
Error 1 ’TestApp.MyList<t>.CopyTo<e>()’ does not define type parameter ’T’ |