public static string CreateUniqueId(IEnumerable <object> parts) { // returns a unique string made up of the pieces passed in StringBuilder builder = new StringBuilder(); foreach (object part in parts) { // We can special-case certain part types MemberInfo memberInfo = part as MemberInfo; if (memberInfo != null) { AppendPartToUniqueIdBuilder(builder, memberInfo.Module.ModuleVersionId); AppendPartToUniqueIdBuilder(builder, memberInfo.MetadataToken); continue; } IUniquelyIdentifiable uniquelyIdentifiable = part as IUniquelyIdentifiable; if (uniquelyIdentifiable != null) { AppendPartToUniqueIdBuilder(builder, uniquelyIdentifiable.UniqueId); continue; } AppendPartToUniqueIdBuilder(builder, part); } return(builder.ToString()); }
/// <summary> /// 根据参数创建唯一标识 /// </summary> /// <param name="parts"></param> /// <returns></returns> public static string CreateUniqueId(IEnumerable <object> parts) { StringBuilder stringBuilder = new StringBuilder(); foreach (object current in parts) { MemberInfo memberInfo = current as MemberInfo; if (memberInfo != null) { DescriptorUtil.AppendPartToUniqueIdBuilder(stringBuilder, memberInfo.Module.ModuleVersionId); DescriptorUtil.AppendPartToUniqueIdBuilder(stringBuilder, memberInfo.MetadataToken.ToString()); } else { IUniquelyIdentifiable uniquelyIdentifiable = current as IUniquelyIdentifiable; if (uniquelyIdentifiable != null) { DescriptorUtil.AppendPartToUniqueIdBuilder(stringBuilder, uniquelyIdentifiable.UniqueId); } else { DescriptorUtil.AppendPartToUniqueIdBuilder(stringBuilder, current); } } } return(stringBuilder.ToString()); }
public ComparisonResults Compare(IUniquelyIdentifiable identifiable) { // Other is null, and we're not - definitely different. if (identifiable == null) { return(ComparisonResults.Different); } if (identifiable is FileInformation other) // Other was a FileInformation, { bool sameFile = other.GetIdentifier().Equals(this.Path); if (!this.ComputeHash(out int selfHash, out Exception failureReason)) { throw new InvalidOperationException(FileInformation.ComputingHashFailed, failureReason); } if (other.ComputeHash(out int otherHash, out Exception dontCare)) { if (otherHash == selfHash) { if (sameFile) // Same path and same hash - we're a match. { return(ComparisonResults.Match); } else // Same hash but different path - we're different, but equivalent. { return(ComparisonResults.DifferentButEquivalent); } } else if (sameFile) // Same path, different hash. We're definitely different and invalid. { return(ComparisonResults.Different | ComparisonResults.Invalidating); } else // Different hash, different path. We're different. { return(ComparisonResults.Different); } } else if (sameFile) // We couldn't compute the hash of the other file, but it has the same path as us. { // We don't know whether we matched or not, but assume we just became invalid. return(ComparisonResults.Invalidating); } else // We couldn't compute the hash of the other file, and it has a different path than us. { // Assume that it was different from us. return(ComparisonResults.Different); } } else // Other wasn't a FileInformation, so we must be different. { return(ComparisonResults.Different); } }
public ComparisonResults Compare(IUniquelyIdentifiable identifiable) { bool selfNull = this.Self == null; bool otherNull = identifiable == null; if (selfNull && otherNull) { return(ComparisonResults.Invalidating); } else if (selfNull ^ otherNull) { return(ComparisonResults.Invalidating); } else { IUniqueIdentifier otherIdentifier = identifiable.GetIdentifier(); HashWrapper selfIdentifier = this.hashWrapper.Value; if (otherIdentifier is HashWrapper wrapper) { bool hashMatch = wrapper.Hash == selfIdentifier.Hash; bool instanceMatch = wrapper.Instance == selfIdentifier.Instance; if (hashMatch && instanceMatch) { return(ComparisonResults.Match); } else if (hashMatch) { return(ComparisonResults.DifferentButEquivalent); } else if (instanceMatch) { return(ComparisonResults.Different | ComparisonResults.Invalidating); } else { return(ComparisonResults.Different); } } else { // Since the other instance wasn't a wrapper, it's (probably) not a match. Also, since we have no // idea what the type we contain is, assume we need to invalidate the cache. return(ComparisonResults.Different | ComparisonResults.Invalidating); } } }
public static void AppendUniqueId(StringBuilder builder, object part) { MemberInfo memberInfo = part as MemberInfo; if (memberInfo != null) { AppendPartToUniqueIdBuilder(builder, memberInfo.Module.ModuleVersionId); AppendPartToUniqueIdBuilder(builder, memberInfo.MetadataToken); return; } IUniquelyIdentifiable uniquelyIdentifiable = part as IUniquelyIdentifiable; if (uniquelyIdentifiable != null) { AppendPartToUniqueIdBuilder(builder, uniquelyIdentifiable.UniqueId); return; } AppendPartToUniqueIdBuilder(builder, part); }