// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Runtime.InteropServices; namespace Microsoft.PowerShell { /// /// Defines a unique key for a Shell Property. /// [StructLayout(LayoutKind.Sequential, Pack = 4)] internal readonly struct PropertyKey : IEquatable { #region Public Properties /// /// A unique GUID for the property. /// public Guid FormatId { get; } /// /// Property identifier (PID) /// public int PropertyId { get; } #endregion #region Public Construction /// /// PropertyKey Constructor. /// /// A unique GUID for the property. /// Property identifier (PID). internal PropertyKey(Guid formatId, int propertyId) { this.FormatId = formatId; this.PropertyId = propertyId; } #endregion #region IEquatable Members /// /// Returns whether this object is equal to another. This is vital for performance of value types. /// /// The object to compare against. /// Equality result. public bool Equals(PropertyKey other) { return other.Equals((object)this); } #endregion #region equality and hashing /// /// Returns the hash code of the object. This is vital for performance of value types. /// /// public override int GetHashCode() { return FormatId.GetHashCode() ^ PropertyId; } /// /// Returns whether this object is equal to another. This is vital for performance of value types. /// /// The object to compare against. /// Equality result. public override bool Equals(object obj) { if (obj == null) return false; if (obj is not PropertyKey) return false; PropertyKey other = (PropertyKey)obj; return other.FormatId.Equals(FormatId) && (other.PropertyId == PropertyId); } /// /// Implements the == (equality) operator. /// /// First property key to compare. /// Second property key to compare. /// True if object a equals object b. false otherwise. public static bool operator ==(PropertyKey propKey1, PropertyKey propKey2) { return propKey1.Equals(propKey2); } /// /// Implements the != (inequality) operator. /// /// First property key to compare. /// Second property key to compare. /// True if object a does not equal object b. false otherwise. public static bool operator !=(PropertyKey propKey1, PropertyKey propKey2) { return !propKey1.Equals(propKey2); } /// /// Override ToString() to provide a user friendly string representation. /// /// String representing the property key. public override string ToString() { return string.Format(System.Globalization.CultureInfo.InvariantCulture, "PropertyKeyFormatString", FormatId.ToString("B"), PropertyId); } #endregion } }