扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:朱先忠 来源:天极网 2007年11月4日
关键字:
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’ |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者