2
\$\begingroup\$

Intrigued by the events chronicled here (also here and here), I saw the inherent utility in having a program-specific GUID available. The SO answers that were correct seemed a bit tightly-coupled and verbose. So I set out to make my own for use. Looking for a critique on the idea itself as well as maintainability, performance, etc. Improvements welcome.

public static class AssemblyExtensions
{
 /// <summary>
 /// The <see cref="GuidAttribute"/> for the entry assembly lazily gotten as a <see cref="Guid"/> if it exists,
 /// <see cref="Guid.Empty"/> otherwise.
 /// </summary>
 private static readonly Lazy<Guid> _MyGuid = new Lazy<Guid>(() => Assembly.GetEntryAssembly().GetGuid());
 /// <summary>
 /// Lazily gets the <see cref="GuidAttribute"/> for the entry assembly as a <see cref="Guid"/> if it exists,
 /// <see cref="Guid.Empty"/> otherwise.
 /// </summary>
 /// <returns>The <see cref="GuidAttribute"/> for the entry assembly as a <see cref="Guid"/> if it exists,
 /// <see cref="Guid.Empty"/> otherwise.</returns>
 public static Guid GetMyGuid() => _MyGuid.Value;
 /// <summary>
 /// Gets the <see cref="GuidAttribute"/> for the given assembly as a <see cref="Guid"/> if it exists,
 /// <see cref="Guid.Empty"/> otherwise.
 /// </summary>
 /// <param name="assembly">The assembly.</param>
 /// <returns>The <see cref="GuidAttribute"/> for the given assembly as a <see cref="Guid"/> if it exists,
 /// <see cref="Guid.Empty"/> otherwise.</returns>
 public static Guid GetGuid(this Assembly assembly) => Guid.TryParse(
 ((GuidAttribute)assembly?.GetCustomAttributes(typeof(GuidAttribute), false).SingleOrDefault())?.Value,
 out Guid guid) ? guid : Guid.Empty;
}
asked Feb 27, 2020 at 1:26
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

Well, there isn't much to say here. But I still see some stuff I don't really like.

The implementations of Lazy<Guid> _MyGuid and Guid GetMyGuid() are quite simple and easy to read and understand. What bothers me here is that for the variable, and both methods the documentation is almost the same. But, at least for me, more critical is the documentation of Guid GetMyGuid() which states "Lazily gets ..." which is clearly an implementation detail which I wouldn't show.

The method Guid GetGuid() is another thing. IMO its very hard to read and understand by sprawling the Guid.TryParse() over 3 lines.

The change I will suggest is a little bit longer but its more readable, at least for me

public static Guid GetGuid(this Assembly assembly)
{
 var guidAttribute = (GuidAttribute)assembly?.GetCustomAttributes(typeof(GuidAttribute), false).SingleOrDefault();
 if(Guid.TryParse(guidAttribute?.Value, out Guid guid)) { return guid; }
 return Guid.Empty;
} 

which could also be written like so

public static Guid GetGuid(this Assembly assembly)
{
 var guidAttribute = (GuidAttribute)assembly?.GetCustomAttributes(typeof(GuidAttribute), false).SingleOrDefault();
 Guid.TryParse(guidAttribute?.Value, out Guid guid); // If Guid.TryParse returns false the out Guid is Guid.Empty
 return guid;
}
answered Feb 27, 2020 at 5:36
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the commentary. Understand and agree with all your points. \$\endgroup\$ Commented Feb 27, 2020 at 14:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.