C# 语法不能理解。

hetengfei_ 2012-03-15 11:17:58
下面的代码是从 .net 库提出来的,读不懂 求解释。

using System.Runtime.InteropServices;
namespace System.Collections
{
// 摘要:
// Exposes the enumerator, which supports a simple iteration over a non-generic
// collection.
[ComVisible(true)] //[1] 这个标志是什么意思
[Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")] //[2] 这个标志是什么意思
public interface IEnumerable
{
// 摘要:
// Returns an enumerator that iterates through a collection.
//
// 返回结果:
// An System.Collections.IEnumerator object that can be used to iterate through
// the collection.
[DispId(-4)] //[3] 这个标志是什么意思
IEnumerator GetEnumerator();
}
}


谁能说说?
谢谢了!!!!!

说说 那三处?
//[1]
//[2]
//[3]
...全文
1638 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq598235031 2012-03-22
  • 打赏
  • 举报
回复
COM组件里面有这些属性,应用的时候需要使用。
bcc222 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 caozhy 的回复:]
不是COM的Attribute,而是这是Attribute,用来给COM互操作包装类提供额外的元数据(metadata)。

要看懂COM,需要C++语言的基础,对COM本身的了解(这些东西没有什么道理,来自微软十几年前的一些约定,甚至更早追溯到OLE时代),有一本叫《COM本质论》的书专门说这个。IDispatch是COM自动化的接口,另外还有一个著名的IUnknown接口,以及其他的接口,……
[/Quote]
大牛
微甜灬呼吸 2012-03-20
  • 打赏
  • 举报
回复
能用不就完了嘛
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 hetengfei_ 的回复:]
也就是说,
如果我想写一个自已的 数据集管现器, 要让它可以实现 foreach 遍历。
那就
要 实现IEnumerable 接口,
要 实现IEnumerable 接口就要实现 GetEnumerator 方法
而 GetEnumerator 方法 又要返回 Enumerator<T> 实例化的对象。
而 Enumerator<T> 类型 又是一个 《迭代器》
《迭代器》……
[/Quote]
default<T> 这个最好理解了 就是给T一个default值 如果T是值类型,就给0 ,如果T是引用类型 就给null
threenewbee 2012-03-16
  • 打赏
  • 举报
回复
之所以这里让 IEnumerable 对COM可见,是互操作的需要,使得那些ActiveX部件也可以兼容 IEnumerable 类型。
threenewbee 2012-03-16
  • 打赏
  • 举报
回复
不是COM的Attribute,而是这是Attribute,用来给COM互操作包装类提供额外的元数据(metadata)。

要看懂COM,需要C++语言的基础,对COM本身的了解(这些东西没有什么道理,来自微软十几年前的一些约定,甚至更早追溯到OLE时代),有一本叫《COM本质论》的书专门说这个。IDispatch是COM自动化的接口,另外还有一个著名的IUnknown接口,以及其他的接口,是在IDL(接口定义语言文件)中描述的,并且被编译器装配起来。这些COM接口非C#里面我们说的接口,虽然概念上是有联系的,但是实现上没有什么关系。

COM学起来是一个非常庞大的东西,而且用C#的话,大部分细节都被隐藏起来,不过要刨根问底,这些东西又不是很容易讲清楚的。只有IE/Shell开发、VBA插件、编写ActiveX部件或者ActiveX文档,Addins程序等等地方才需要这些知识。说实话,这些东西用得越来越少,这些技术也逐渐边缘化了。
ghjlesse 2012-03-16
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 hetengfei_ 的回复:]
也就是说,
如果我想写一个自已的 数据集管现器, 要让它可以实现 foreach 遍历。
那就
要 实现IEnumerable 接口,
要 实现IEnumerable 接口就要实现 GetEnumerator 方法
而 GetEnumerator 方法 又要返回 Enumerator<T> 实例化的对象。
而 Enumerator<T> 类型 又是一个 《迭代器》
《迭代器》……
[/Quote]
那个是属性,可以理解为默认属性,其实就是方法。
接口就是一个壳,里面什么都没有;
迭代器就是若干个方法组成的一系列操作,例如:发动机是由好多个零件组成的;
你要自己写迭代器的话,要实现IEnumerable接口。如果用VS2008的话,你写完这个接口再按下回车,VS会自动把你需要实现的方法给你写出来,你可以试试。
写一句比总结十句有用。
hetengfei_ 2012-03-16
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 sunzongbao2007 的回复:]
lz的行为一直不像是个技术人员。
[/Quote]
那象个啥?
hetengfei_ 2012-03-16
  • 打赏
  • 举报
回复
也就是说,
如果我想写一个自已的 数据集管现器, 要让它可以实现 foreach 遍历。
那就
要 实现IEnumerable 接口,
要 实现IEnumerable 接口就要实现 GetEnumerator 方法
而 GetEnumerator 方法 又要返回 Enumerator<T> 实例化的对象。
而 Enumerator<T> 类型 又是一个 迭代器
《迭代器》有下面又有几个方法:
public bool MoveNext();//移到下一节点
private bool MoveNextRare();// 这个应该是其它版本 用的吧。
void IEnumerator.Reset(); //应得是个 复位 功能。
。。。真是复杂。

这些方法中 有一个[东东]看不懂的

如下面:
this.current = default(T);// 这句话是什么意思,default 关键字,有什么作用?

铜臂阿铁木 2012-03-16
  • 打赏
  • 举报
回复
lz的行为一直不像是个技术人员。
铜臂阿铁木 2012-03-16
  • 打赏
  • 举报
回复
lz的发帖一直很绚烂
铜臂阿铁木 2012-03-16
  • 打赏
  • 举报
回复
lz的头像一直以来都很耀眼
threenewbee 2012-03-16
  • 打赏
  • 举报
回复
抛开那些版本判断、异常处理等等额外的东西不谈,这个代码是很容易理解的。

你可以看《设计模式》的迭代器模式,一个迭代器由三个基本操作组成,分别是重置迭代器,取当前,将游标移动到下一个(并判断是否到结尾)。
hetengfei_ 2012-03-16
  • 打赏
  • 举报
回复
好象.net 藏得很深啊!
hetengfei_ 2012-03-16
  • 打赏
  • 举报
回复
我用 reflector 在List <T>中看到了
GetEnumerator();// 方法
public Enumerator<T> GetEnumerator()
{
return new Enumerator<T>((List<T>) this);
}


些方法返回一个 Enumerator<T>类型;
于是我又打开 Enumerator<T> 的定义:
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{
private List<T> list;
private int index;
private int version;
private T current;
internal Enumerator(List<T> list)
{
this.list = list;
this.index = 0;
this.version = list._version;
this.current = default(T);
}
public void Dispose()//打开发现这个方法没有现实体
{
}
public bool MoveNext()
{
List<T> list = this.list;
if ((this.version == list._version) && (this.index < list._size))
{
this.current = list._items[this.index];
this.index++;
return true;
}
return this.MoveNextRare();
}
private bool MoveNextRare()
{
if (this.version != this.list._version)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}
this.index = this.list._size + 1;
this.current = default(T);
return false;
}
public T Current
{
get
{
return this.current;
}
}
object IEnumerator.Current
{
get
{
if ((this.index == 0) || (this.index == (this.list._size + 1)))
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
}
return this.Current;
}
}
void IEnumerator.Reset()
{
if (this.version != this.list._version)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}
this.index = 0;
this.current = default(T);
}
}


//还是看得不懂


wanghui0380 2012-03-16
  • 打赏
  • 举报
回复
接口只是接口,他并不带实现

所以他的实现在具体类型上

比如List<string> 的就在List<T> 这个类里面
hetengfei_ 2012-03-16
  • 打赏
  • 举报
回复
请问如何见得 :
IEnumerator GetEnumerator();


的实现体?
//在这两个类型中:
int[] )( List<string>)
hetengfei_ 2012-03-16
  • 打赏
  • 举报
回复
原来是这样啊,[不是C# 的 范筹]

我们可以看到 接口IEnumerable 有一个方法:

//这个函数的返回值是: 返回实现了 IEnumerator 接口 的对象;[如下声明]
IEnumerator GetEnumerator();

//你看下面代码: (int[] )( List<string>) 这样个类型都实现了IEnumerable接口,
//可我见不到具体的实现代码,---就是函数IEnumerator GetEnumerator();在类型(int[] )( List<string>)中的定义。

IEnumerable ieInt = new int[] { 1, 2, 3, 4 };
IEnumerable ieString = new List<string> {"小菜","大侠","高手" ,"苯旦"};



wanghui0380 2012-03-16
  • 打赏
  • 举报
回复
1,2,3都不必说,lz去看一下com的接口规范就明白了

这3个都是按照com的接口规范标准的声明,至于原理!其实没有任何原理,也没有任何理由,当初那个规范标准那么要求的,所以就按那个要求做。

对于规范,要求来说不必说原理了,如果我要求你只能传bool值给我,那么你就不能传int给我,这中间没有任何道理可言
kirinboy 2012-03-16
  • 打赏
  • 举报
回复
在你眼里,IEnumerable的定义应该是这个样子的

using System.Runtime.InteropServices;
namespace System.Collections
{
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
}

知道这些就足够了
加载更多回复(10)

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧