Skip to content

Commit 04a1591

Browse files
nem0ddobrev
authored andcommitted
Fixed the generated C# for fixed arrays of Booleans.
fixes #1004 * mend Fixed the generated C# for fixed arrays of Booleans
1 parent 481bbc0 commit 04a1591

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
115115
}
116116
else
117117
{
118-
if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
118+
if (arrayType.IsPrimitiveType(PrimitiveType.Bool))
119+
supportBefore.WriteLineIndent($@"{value}[i] = {
120+
Context.ReturnVarName}[i] != 0;");
121+
else if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
119122
Context.Context.Options.MarshalCharAsManagedChar)
120123
supportBefore.WriteLineIndent($@"{value}[i] = global::System.Convert.ToChar({
121124
Context.ReturnVarName}[i]);");
@@ -514,7 +517,11 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
514517
}
515518
else
516519
{
517-
if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
520+
if (arrayType.IsPrimitiveType(PrimitiveType.Bool))
521+
supportBefore.WriteLineIndent($@"{
522+
Context.ReturnVarName}[i] = (byte)({
523+
Context.ArgName}[i] ? 1 : 0);");
524+
else if (arrayType.IsPrimitiveType(PrimitiveType.Char) &&
518525
Context.Context.Options.MarshalCharAsManagedChar)
519526
supportBefore.WriteLineIndent($@"{
520527
Context.ReturnVarName}[i] = global::System.Convert.ToSByte({

tests/CSharp/CSharp.Tests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,24 @@ public void TestCopyCtor()
281281
}
282282
}
283283

284+
[Test]
285+
public void TestBooleanArray()
286+
{
287+
Foo foo = new Foo { A = 10 };
288+
var new_values = new bool[5];
289+
for(int i = 0; i < new_values.Length; ++i)
290+
{
291+
new_values[i] = i % 2 == 0;
292+
}
293+
foo.Btest = new_values;
294+
Assert.AreEqual(true, foo.Btest[0]);
295+
Assert.AreEqual(false, foo.Btest[1]);
296+
Assert.AreEqual(true, foo.Btest[2]);
297+
Assert.AreEqual(false, foo.Btest[3]);
298+
Assert.AreEqual(true, foo.Btest[4]);
299+
}
300+
301+
284302
[Test]
285303
public void TestImplicitCtor()
286304
{

tests/CSharp/CSharp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class DLL_API Foo
3232
int operator ++();
3333
int operator --();
3434

35+
bool btest[5];
36+
3537
protected:
3638
int P;
3739
TemplateInAnotherUnit<int> templateInAnotherUnit;

0 commit comments

Comments
 (0)