Skip to content

Commit 830bf62

Browse files
author
Denis Lebedenko
committed
Allow casting arrays to ICollection, implement .Count
1 parent ff33be3 commit 830bf62

4 files changed

Lines changed: 25 additions & 4 deletions

File tree

Libraries/JSIL.Bootstrap.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,21 @@ JSIL.MakeClass("System.Object", "JSIL.ArrayInterfaceOverlay", true, ["T"], funct
773773
.Overrides(1, "GetEnumerator");
774774

775775
// FIXME: Implement actual members of IList.
776+
777+
$.Method({Static:false, Public:true }, "get_Count",
778+
new JSIL.MethodSignature($.Int32, [], []),
779+
function get_Count () {
780+
return this._array.length;
781+
}
782+
);
776783

777784
$.ImplementInterfaces(
778785
/* 0 */ $jsilcore.TypeRef("System.Collections.IEnumerable"),
779786
/* 1 */ $jsilcore.TypeRef("System.Collections.Generic.IEnumerable`1", [T]),
780-
/* 2 */ $jsilcore.TypeRef("System.Collections.IList"),
781-
/* 3 */ $jsilcore.TypeRef("System.Collections.Generic.IList`1", [T])
787+
/* 2 */ $jsilcore.TypeRef("System.Collections.ICollection"),
788+
/* 3 */ $jsilcore.TypeRef("System.Collections.Generic.ICollection`1", [T]),
789+
/* 4 */ $jsilcore.TypeRef("System.Collections.IList"),
790+
/* 5 */ $jsilcore.TypeRef("System.Collections.Generic.IList`1", [T])
782791
);
783792
});
784793

Libraries/JSIL.Core.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4351,12 +4351,13 @@ JSIL.$ActuallyMakeCastMethods = function (publicInterface, typeObject, specialTy
43514351
};
43524352

43534353
var isIEnumerable = typeName.indexOf(".IEnumerable") >= 0;
4354+
var isICollection = typeName.indexOf(".ICollection") >= 0;
43544355
var isIList = typeName.indexOf(".IList") >= 0;
43554356

43564357
var isInterface = typeObject.IsInterface || false;
43574358

43584359
// HACK: Handle casting arrays to IEnumerable by creating an overlay.
4359-
if (isIEnumerable || isIList) {
4360+
if (isIEnumerable || isICollection || isIList) {
43604361
checkMethod = function Check_ArrayInterface (value) {
43614362
// FIXME: IEnumerable<int>.Is(float[]) will return true.
43624363
if (JSIL.IsArray(value))
@@ -4560,7 +4561,7 @@ JSIL.$ActuallyMakeCastMethods = function (publicInterface, typeObject, specialTy
45604561
};
45614562
}
45624563

4563-
if (isIEnumerable || isIList) {
4564+
if (isIEnumerable || isICollection || isIList) {
45644565
var innerAsFunction = asFunction;
45654566
var innerCastFunction = castFunction;
45664567

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
static class Program {
5+
public static void Main () {
6+
int[] arr = new int[] { 2, 3, 5, 7, 11 };
7+
ICollection<int> col = arr;
8+
Console.WriteLine("Count: {0}", col.Count);
9+
}
10+
}

Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
<None Include="SimpleTestCases\Issue245.cs" />
233233
<None Include="PerformanceTestCases\Sieve.cs" />
234234
<None Include="SimpleTestCases\ListIndexOfEnum.cs" />
235+
<None Include="SimpleTestCases\ArrayICollection.cs" />
235236
<Compile Include="TypeInformationTests.cs" />
236237
<None Include="TestCases\LongArithmetic.cs" />
237238
<None Include="InterfaceTestCases\NestedInterfaces.cs" />

0 commit comments

Comments
 (0)