-
Notifications
You must be signed in to change notification settings - Fork 4k
/
ProjectId.cs
102 lines (83 loc) · 3.36 KB
/
ProjectId.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
#pragma warning disable CA1200 // Avoid using cref tags with a prefix
/// <summary>
/// An identifier that can be used to refer to the same <see cref="Project"/> across versions.
/// </summary>
/// <remarks>
/// This supports the general message-pack <see cref="DataContractAttribute"/> of being serializable. However, in
/// practice, this is not serialized directly, but through the use of a custom formatter <see
/// cref="T:Microsoft.CodeAnalysis.Remote.MessagePackFormatters.ProjectIdFormatter"/>
/// </remarks>
#pragma warning restore CA1200 // Avoid using cref tags with a prefix
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
[DataContract]
public sealed class ProjectId : IEquatable<ProjectId>, IObjectWritable
{
/// <summary>
/// The system generated unique id.
/// </summary>
[DataMember(Order = 0)]
public Guid Id { get; }
[DataMember(Order = 1)]
private readonly string? _debugName;
private ProjectId(Guid guid, string? debugName)
{
this.Id = guid;
_debugName = debugName;
}
/// <summary>
/// Create a new ProjectId instance.
/// </summary>
/// <param name="debugName">An optional name to make this id easier to recognize while debugging.</param>
public static ProjectId CreateNewId(string? debugName = null)
=> new(Guid.NewGuid(), debugName);
public static ProjectId CreateFromSerialized(Guid id, string? debugName = null)
{
if (id == Guid.Empty)
{
throw new ArgumentException(nameof(id));
}
return new ProjectId(id, debugName);
}
internal string? DebugName => _debugName;
private string GetDebuggerDisplay()
=> string.Format("({0}, #{1} - {2})", this.GetType().Name, this.Id, _debugName);
public override string ToString()
=> GetDebuggerDisplay();
public override bool Equals(object? obj)
=> this.Equals(obj as ProjectId);
public bool Equals(ProjectId? other)
{
return
other is object &&
this.Id == other.Id;
}
public static bool operator ==(ProjectId? left, ProjectId? right)
=> EqualityComparer<ProjectId?>.Default.Equals(left, right);
public static bool operator !=(ProjectId? left, ProjectId? right)
=> !(left == right);
public override int GetHashCode()
=> this.Id.GetHashCode();
bool IObjectWritable.ShouldReuseInSerialization => true;
void IObjectWritable.WriteTo(ObjectWriter writer)
{
writer.WriteGuid(Id);
writer.WriteString(DebugName);
}
internal static ProjectId ReadFrom(ObjectReader reader)
{
var guid = reader.ReadGuid();
var debugName = reader.ReadString();
return CreateFromSerialized(guid, debugName);
}
}
}