Skip to content

Commit e44d82d

Browse files
authored
Fix usage of Empty field and Guid constructor from string (#244)
1 parent 821ac7b commit e44d82d

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

Tests/NFUnitTestSystemLib/UnitTestGuid.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,5 +437,24 @@ public void ToByteArray()
437437

438438
CollectionAssert.AreEqual(new byte[] { 0xd5, 0x10, 0xa1, 0xa8, 0x49, 0xfc, 0xc5, 0x43, 0xbf, 0x46, 0x80, 0x2d, 0xb8, 0xf8, 0x43, 0xff }, myGuidAsArray);
439439
}
440+
441+
[TestMethod]
442+
public void GuidAsStaticField()
443+
{
444+
var guid1 = ClassWithGuid.BaseUuid1;
445+
var guid2 = ClassWithGuid.BaseUuid2;
446+
447+
Assert.AreEqual(new Guid("00000000-0000-1000-8000-00805f9b34fb"), guid1);
448+
CollectionAssert.AreEqual(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }, guid2);
449+
}
450+
451+
internal class ClassWithGuid
452+
{
453+
private static Guid baseUuid1 = new Guid("00000000-0000-1000-8000-00805f9b34fb");
454+
private static byte[] baseUuid2 = new Guid("00000000-0000-1000-8000-00805f9b34fb").ToByteArray();
455+
456+
public static Guid BaseUuid1 => baseUuid1;
457+
public static byte[] BaseUuid2 => baseUuid2;
458+
}
440459
}
441460
}

nanoFramework.CoreLibrary/System/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
[assembly: AssemblyProduct(".NET nanoFramework mscorlib")]
1111
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]
1212

13-
[assembly: AssemblyNativeVersion("100.5.0.23")]
13+
[assembly: AssemblyNativeVersion("100.5.0.24")]

nanoFramework.CoreLibrary/System/Guid.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,7 @@ public struct Guid
1616
/// <summary>
1717
/// A read-only instance of the Guid class which consists of all zeros.
1818
/// </summary>
19-
public static readonly Guid Empty = new Guid(
20-
0,
21-
0,
22-
0,
23-
0,
24-
0,
25-
0,
26-
0,
27-
0,
28-
0,
29-
0,
30-
0);
19+
public static readonly Guid Empty = new Guid(new byte[16]);
3120

3221
/// <summary>
3322
/// Initializes a new instance of the <see cref="Guid"/> structure by using the specified integers and bytes.
@@ -154,13 +143,12 @@ public Guid(string g)
154143
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
155144
if (!TryParseGuidWithDashes(
156145
g,
157-
out Guid result))
146+
out this))
158147
{
159148
throw new ArgumentException();
160149
}
161150
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
162151

163-
this = result;
164152
}
165153

166154
/// <summary>
@@ -343,7 +331,7 @@ public static bool TryParseGuidWithDashes(
343331

344332
// because this is a struct we can't assign the Empty directly to result,
345333
// otherwise it will overwrite the _data field of the struct, as this is a shallow copy
346-
result = new Guid(Empty.ToByteArray());
334+
result = new Guid(new byte[16]);
347335

348336
// Check for optional surrounding braces
349337
if (input[0] == '{')

0 commit comments

Comments
 (0)