If my class implements IEnumerable
, I can use VBScript's For Each
loop:
[ComVisible(true)]
[ProgId("Stackoverflow.MyIssues")]
[Guid("7D392CB1-9080-49D0-B9CE-05B214B2C448")]
public class MyIssue : IEnumerable
{
readonly List<string> issues = new List<string>(new string[] { "foo", "bar" });
public string this[int index]
{
get { return issues[index]; }
}
public IEnumerator GetEnumerator()
{
return issues.GetEnumerator();
}
}
Dim o : Set o = CreateObject("Stackoverflow.MyIssues")
Dim i
For Each i In o
WScript.Echo i
Next
If I change the interface to IEnumerable<string>
(so C#'s foreach
loop uses string
instead of object
):
public class MyIssue : IEnumerable<string>
and replace GetEnumerator
with:
public IEnumerator<string> GetEnumerator()
{
return issues.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
The scritp will fail with an error:
Object Doesn't Support this Property or Method
My understanding is that the public GetEnumerator()
is not exported, since it
is a generic method, and the IEnumerable.GetEnumerator
is not exported, since
my instance must first be casted to IEnumerable
but in VBScript one cannot
cast objects.
Is this true?
Is it possible to tell the compiler that IEnumerable.GetEnumerator
should be
exported as public IEnumerable GetEnumerator
? (Or does this statement make no
sense? etc.)