aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/Message.cs
blob: 85a402e53196f5f1d4cfab8c563a09eee7607eb4 (plain)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Data
{
    using System;
    using System.Resources;

    /// <summary>
    /// Event args for message events.
    /// </summary>
    public class Message
    {
        /// <summary>
        /// Creates a new Message using a format string.
        /// </summary>
        /// <param name="sourceLineNumbers">Source line numbers for the message.</param>
        /// <param name="level">Message level.</param>
        /// <param name="id">Id for the message.</param>
        /// <param name="format">Format .</param>
        /// <param name="messageArgs">Arguments for the format string.</param>
        public Message(SourceLineNumber sourceLineNumbers, MessageLevel level, int id, string format, params object[] messageArgs)
        {
            this.SourceLineNumbers = sourceLineNumbers;
            this.Level = level;
            this.Id = id;
            this.ResourceNameOrFormat = format;
            this.MessageArgs = messageArgs;
        }

        /// <summary>
        /// Creates a new Message using a format string from a resource manager.
        /// </summary>
        /// <param name="sourceLineNumbers">Source line numbers for the message.</param>
        /// <param name="level">Message level.</param>
        /// <param name="id">Id for the message.</param>
        /// <param name="resourceManager">Resource manager.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="messageArgs">Arguments for the format string.</param>
        public Message(SourceLineNumber sourceLineNumbers, MessageLevel level, int id, ResourceManager resourceManager, string resourceName, params object[] messageArgs)
        {
            this.SourceLineNumbers = sourceLineNumbers;
            this.Level = level;
            this.Id = id;
            this.ResourceManager = resourceManager;
            this.ResourceNameOrFormat = resourceName;
            this.MessageArgs = messageArgs;
        }

        /// <summary>
        /// Gets the source line numbers.
        /// </summary>
        /// <value>The source line numbers.</value>
        public SourceLineNumber SourceLineNumbers { get; }

        /// <summary>
        /// Gets the Id for the message.
        /// </summary>
        /// <value>The Id for the message.</value>
        public int Id { get; }

        /// <summary>
        /// Gets the resource manager for this event args.
        /// </summary>
        /// <value>The resource manager for this event args.</value>
        public ResourceManager ResourceManager { get; }

        /// <summary>
        /// Gets the name of the resource or format string if no resource manager was provided.
        /// </summary>
        /// <value>The name of the resource or format string.</value>
        public string ResourceNameOrFormat { get; }

        /// <summary>
        /// Gets or sets the <see cref="MessageLevel"/> for the message.
        /// </summary>
        /// <value>The <see cref="MessageLevel"/> for the message.</value>
        public MessageLevel Level { get; private set; }

        /// <summary>
        /// Gets the arguments for the format string.
        /// </summary>
        /// <value>The arguments for the format string.</value>
        public object[] MessageArgs { get; }

        public override string ToString()
        {
            if (this.ResourceManager == null)
            {
                return String.Format(this.ResourceNameOrFormat, this.MessageArgs);
            }
            else
            {
                return String.Format(this.ResourceManager.GetString(this.ResourceNameOrFormat), this.MessageArgs);
            }
        }
    }
}