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;
}
1 Answer 1
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;
}
-
\$\begingroup\$ Thank you for the commentary. Understand and agree with all your points. \$\endgroup\$Jesse C. Slicer– Jesse C. Slicer2020年02月27日 14:18:16 +00:00Commented Feb 27, 2020 at 14:18