VB6到C#:IUnknown
我试图将VB6中的一个属性转换为C#。详情如下:VB6到C#:IUnknown,c#,properties,vb6,vb6-migration,iunknown,C#,Properties,Vb6,Vb6 Migration,Iunknown,我试图将VB6中的一个属性转换为C#。详情如下: Public Property Get NewEnum() As IUnknown 'this property allows you to enumerate 'this collection with the For...Each syntax Set NewEnum = m_coll.[_NewEnum] End Property m_coll是一个私有变量,它现在是一个ArrayList而不是前一个集合 m_co
Public Property Get NewEnum() As IUnknown
'this property allows you to enumerate
'this collection with the For...Each syntax
Set NewEnum = m_coll.[_NewEnum]
End Property
m_coll
是一个私有变量,它现在是一个ArrayList
而不是前一个集合
m_coll
正在填充我自己的一个类对象。如您所见,此属性的类型为IUnknown
在这一点上,我可能没有正确地思考,但是如果您希望能够对一个类进行foreach(就像在vb6中将NewEnum()作为IUnknown公开一样),那么在C#?中是否有类似于这种属性的属性,您可以让您的类实现IEnumerable-例如:
public class MyClass : IEnumerable
{
private List<string> items = new List<string>();
public MyClass()
{
items.Add("first");
items.Add("second");
}
public IEnumerator GetEnumerator()
{
return items.GetEnumerator();
}
}
为了简单起见,我使用了
List
,但是你可以使用List
你在考虑IEnumerable吗?这允许您对集合使用foreach语句。您不应使用ArrayList
。改用通用的列表
。我已经弄明白了你的答案,但我仍然会告诉你答案;)谢谢谢谢对不起,我没有更快:)
MyClass myClass =new MyClass();
foreach (var itm in myClass)
{
Console.WriteLine(itm);
}