Skip to content

Fix usage of Empty field and Guid constructor from string #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Tests/NFUnitTestSystemLib/UnitTestGuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,24 @@ public void ToByteArray()

CollectionAssert.AreEqual(new byte[] { 0xd5, 0x10, 0xa1, 0xa8, 0x49, 0xfc, 0xc5, 0x43, 0xbf, 0x46, 0x80, 0x2d, 0xb8, 0xf8, 0x43, 0xff }, myGuidAsArray);
}

[TestMethod]
public void GuidAsStaticField()
{
var guid1 = ClassWithGuid.BaseUuid1;
var guid2 = ClassWithGuid.BaseUuid2;

Assert.AreEqual(new Guid("00000000-0000-1000-8000-00805f9b34fb"), guid1);
CollectionAssert.AreEqual(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }, guid2);
}

internal class ClassWithGuid
{
private static Guid baseUuid1 = new Guid("00000000-0000-1000-8000-00805f9b34fb");
private static byte[] baseUuid2 = new Guid("00000000-0000-1000-8000-00805f9b34fb").ToByteArray();

public static Guid BaseUuid1 => baseUuid1;
public static byte[] BaseUuid2 => baseUuid2;
}
}
}
2 changes: 1 addition & 1 deletion nanoFramework.CoreLibrary/System/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
[assembly: AssemblyProduct(".NET nanoFramework mscorlib")]
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]

[assembly: AssemblyNativeVersion("100.5.0.23")]
[assembly: AssemblyNativeVersion("100.5.0.24")]
18 changes: 3 additions & 15 deletions nanoFramework.CoreLibrary/System/Guid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,7 @@ public struct Guid
/// <summary>
/// A read-only instance of the Guid class which consists of all zeros.
/// </summary>
public static readonly Guid Empty = new Guid(
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0);
public static readonly Guid Empty = new Guid(new byte[16]);

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

this = result;
}

/// <summary>
Expand Down Expand Up @@ -343,7 +331,7 @@ public static bool TryParseGuidWithDashes(

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

// Check for optional surrounding braces
if (input[0] == '{')
Expand Down
Loading