ProjectId.cs 2.9 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5

using System;
using System.Collections.Generic;
using System.Diagnostics;
6
using Roslyn.Utilities;
P
Pilchie 已提交
7 8 9 10

namespace Microsoft.CodeAnalysis
{
    /// <summary>
11
    /// An identifier that can be used to refer to the same <see cref="Project"/> across versions.
P
Pilchie 已提交
12
    /// </summary>
13
    [DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
14
    public sealed class ProjectId : IEquatable<ProjectId>, IObjectWritable
P
Pilchie 已提交
15
    {
16
        private readonly string _debugName;
P
Pilchie 已提交
17 18 19 20

        /// <summary>
        /// The system generated unique id.
        /// </summary>
21
        public Guid Id { get; }
P
Pilchie 已提交
22

23
        private ProjectId(Guid guid, string debugName)
24 25
        {
            this.Id = guid;
26
            _debugName = debugName;
27 28
        }

P
Pilchie 已提交
29 30 31 32 33 34
        /// <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)
        {
35
            return new ProjectId(Guid.NewGuid(), debugName);
P
Pilchie 已提交
36 37
        }

J
Jared Parsons 已提交
38 39 40 41 42 43 44
        public static ProjectId CreateFromSerialized(Guid id, string debugName = null)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentException(nameof(id));
            }

J
Jared Parsons 已提交
45
            return new ProjectId(id, debugName);
J
Jared Parsons 已提交
46 47
        }

48 49
        internal string DebugName => _debugName;

50
        private string GetDebuggerDisplay()
P
Pilchie 已提交
51
        {
52
            return string.Format("({0}, #{1} - {2})", this.GetType().Name, this.Id, _debugName);
P
Pilchie 已提交
53 54 55 56
        }

        public override string ToString()
        {
57
            return GetDebuggerDisplay();
P
Pilchie 已提交
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
        }

        public override bool Equals(object obj)
        {
            return this.Equals(obj as ProjectId);
        }

        public bool Equals(ProjectId other)
        {
            return
                !ReferenceEquals(other, null) &&
                this.Id == other.Id;
        }

        public static bool operator ==(ProjectId left, ProjectId right)
        {
            return EqualityComparer<ProjectId>.Default.Equals(left, right);
        }

        public static bool operator !=(ProjectId left, ProjectId right)
        {
            return !(left == right);
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
86

V
vsadov 已提交
87
        bool IObjectWritable.ShouldReuseInSerialization => true;
88

89 90
        void IObjectWritable.WriteTo(ObjectWriter writer)
        {
91
            writer.WriteGuid(Id);
92 93 94 95 96
            writer.WriteString(DebugName);
        }

        internal static ProjectId ReadFrom(ObjectReader reader)
        {
97
            var guid = reader.ReadGuid();
98 99 100 101
            var debugName = reader.ReadString();

            return CreateFromSerialized(guid, debugName);
        }
P
Pilchie 已提交
102
    }
103
}