- 热门文章:
- · 用ASP.NET 1.1 新特征防止Script攻击
- · 关于ASP.Net中的时间处理
- · javascript实现的数据表格:冻结列、调整列宽和客户端排序
- · [常见问题]cookie使用1.Page与HttpContext的Request、Response
- · 怎样在页面和页面的用户控件进行交互
- · webform页面间传值的特殊方法
- · 怎么直接建立一个DataTable并且为之添加数据
- · 我的第一个Asp.Net程序
- · 如何把数据放到web不能访问的文件夹中并给用户下载?
- · How to handle the concurrency problems on ASP.Net Database
- · 后台动态设置前台标签内容和属性
- · ASP.NET中让同一个页面不同的文本框回车响应不同的事件
上一篇:DotNetNuke(DNN) >>
枚举器模式(Enumerator pattern)
如何使用枚举器模式(Enumerator pattern):
1、 如果你要实现IEnumerable.GetEnumerator,你也要实现一个GetEnumerator方法的非虚方法版本。你的枚举器类的IEnumerable.GetEnumerator方法应该调用这个非虚方法。如下所示应该返回一个内嵌的枚举器结构:
· class MyClass : IEnumerable
· {
· // non-virtual implementation for your custom collection
· public MyEnumerator GetEnumerator() {
· return new MyEnumerator(this); // Return nested public struct
· }
· // IEnumerator implementation
· public IEnumerator.GetEnumerator() {
· return GetEnumerator();//call the non-interface method
· }
· }
如果你的类已经显示地提供了非虚的GetEnumerator方法,那么foreach语句就会调用这个非虚的GetEnumerator方法。否则,它会调用IEnumerable.GetEnumerator方法,如果你的类实现了Ienumerable接口。调用一个非虚方法比通过接口调用一个虚方法要更有效。
2、 在枚举器结构中显示的实现IEnumerator.Current属性。.NET的实现类在实现时会使得该属性返回一个System.Object对象,而不是一个强类型的对象。这会存在类型转换开销。通过在你的Current属性中返回一个强类型对象或者精确的值类型而不是System.Object,可以避免类型转换的开销。因为你已经显示地实现了一个非虚地GetEnumerator方法(不是IEnumerable.GetEnumerator方法),CLR能够直接调用Enumerator.Current属性,而不是调用IEnumerator.Current属性,因此可以直接获取所期待的数据,还能避免类型转换和装箱操作的开销。而减少虚方法调用,可以利用方法的内联了。你的实现应该如下:
// Custom property in your class
//call this property to avoid the boxing or casting overhead
Public MyValueType Current {
MyValueType obj = new MyValueType();
// the obj fields are populated here
return obj;
}
// Explicit member implementation
Object IEnumerator.Current {
get { return Current} // Call the non-interface property to avoid casting
}
注:枚举器模式只有当性能是关键时才适用。
下面的示例代码展示了枚举器模式:
public class ItemTypeCollection: IEnumerable
{
public struct MyEnumerator : IEnumerator
{
public ItemType Current { get {… } }
object IEnumerator.Current { get { return Current; } }
public bool MoveNext() { … }
… }
public MyEnumerator GetEnumerator() { … }
IEnumerator IEnumerable.GetEnumerator() { … }
…}
要利用JIT的内联方式,应该避免在你的集合类中使用虚方法,除非你确实需要扩展。同时,在Current属性的实现中返回当前值来允许使用内联,或者使用一个字段。
下一篇:用ASP.NET 1.1 新特征防止Script攻击 >>
相关文章:
- · 认识ASP.NET配置文件Web.config
- · 将ArrayList中的ListItem绑定到DropDownList中去
- · 安全存放web项目数据库连接字符串
- · 分享:Project级别的权限控制
- · Solidworks二次开发—06—在装配体中添加配合
- · 在Repeater中嵌套使用Repeater
- · 一个简单的加密/解密方法
- · 加入身份验证信息的SMTP mail发送
- · webconfig的设置节点说明
- · 现有的Web打印控制技术分成几种方案
- · 一段实现DataGrid的“编辑”、“取消”功能脚本[无刷新]
- · WEB图片高清晰浏览同打印
- · Tangram与软件的组合构造
- · solidworks二次开发-04-修改数据
- · Solidworks二次开发-05-装配体中插入零部件
- · solidworks二次开发-03-访问特征数据
- · solidworks二次开发-02-用来访问特征的两个API
- · solidworks二次开发-01-录制一个宏
- · 有关于web播放器的列表播放问题
- · Microsoft User Interface Process Application Block 研究(3)
- · ASP.NET中使用IFRAME建立类Modal窗口
- · 挤压造型Extrusion的节点说明和应用实例
- · .net 里面 protected private 的变量也可以访问
- · 怎样得到一个系统盘的全名,不是字符,是全名,如:本地磁盘(C:)?
- · ASP.NET中新的代码编译功能(三)
- · ASP.NET中新的代码编译功能(二)
- · ASP.NET中新的代码编译功能(一)
- · asp.net里,一个小的自定义错误显示
- · ASP.NET编程中的十大技巧
- · 在ASP.NET中面向对象的编程思想
- · 有史以来最牛B的.NET程序集加密方法?
- · 在ASP.NET中实现多文件上传
- · (论坛答疑点滴)怎么给Table动态添加控件并且得到控件的值?
- · (论坛答疑点滴)有的时候DataGrid取值取不到?
- · (论坛答疑点滴)联合主键的情况怎么在DataGrid中利用DataKeys定位记录?
- · (论坛答疑点滴)__doPostBack()无效?
- · ASP.net生成文字图片
- · 自定义类(ASP.NET_VB)
