1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.ComponentModel.Composition.Hosting;
\r
6 using System.ComponentModel.Composition.Primitives;
\r
7 using System.Diagnostics;
\r
8 using System.Globalization;
\r
9 using System.Security.Permissions;
\r
10 using Microsoft.Internal;
\r
12 namespace System.ComponentModel.Composition
\r
15 /// Represents an error that occurs during composition in a <see cref="CompositionContainer"/>.
\r
18 [DebuggerTypeProxy(typeof(CompositionErrorDebuggerProxy))]
\r
19 public class CompositionError : ICompositionError
\r
21 private readonly CompositionErrorId _id;
\r
22 private readonly string _description;
\r
23 private readonly Exception _exception;
\r
25 private readonly ICompositionElement _element;
\r
28 /// Initializes a new instance of the <see cref="CompositionError"/> class
\r
29 /// with the specified error message.
\r
31 /// <param name="message">
\r
32 /// A <see cref="String"/> containing a message that describes the
\r
33 /// <see cref="CompositionError"/>; or <see langword="null"/> to set the
\r
34 /// <see cref="Description"/> property to an empty string ("").
\r
36 public CompositionError(string message)
\r
37 : this(CompositionErrorId.Unknown, message, (ICompositionElement)null, (Exception)null)
\r
42 /// Initializes a new instance of the <see cref="CompositionError"/> class
\r
43 /// with the specified error message and composition element that is the
\r
44 /// cause of the composition error.
\r
46 /// <param name="element">
\r
47 /// The <see cref="ICompositionElement"/> that is the cause of the
\r
48 /// <see cref="CompositionError"/>; or <see langword="null"/> to set
\r
49 /// the <see cref="CompositionError.Element"/> property to
\r
50 /// <see langword="null"/>.
\r
52 /// <param name="message">
\r
53 /// A <see cref="String"/> containing a message that describes the
\r
54 /// <see cref="CompositionError"/>; or <see langword="null"/> to set the
\r
55 /// <see cref="Description"/> property to an empty string ("").
\r
57 public CompositionError(string message, ICompositionElement element)
\r
58 : this(CompositionErrorId.Unknown, message, element, (Exception)null)
\r
63 /// Initializes a new instance of the <see cref="CompositionError"/> class
\r
64 /// with the specified error message and exception that is the cause of the
\r
65 /// composition error.
\r
67 /// <param name="message">
\r
68 /// A <see cref="String"/> containing a message that describes the
\r
69 /// <see cref="CompositionError"/>; or <see langword="null"/> to set the
\r
70 /// <see cref="Description"/> property to an empty string ("").
\r
72 /// <param name="exception">
\r
73 /// The <see cref="Exception"/> that is the underlying cause of the
\r
74 /// <see cref="CompositionError"/>; or <see langword="null"/> to set
\r
75 /// the <see cref="CompositionError.Exception"/> property to <see langword="null"/>.
\r
77 public CompositionError(string message, Exception exception)
\r
78 : this(CompositionErrorId.Unknown, message, (ICompositionElement)null, exception)
\r
83 /// Initializes a new instance of the <see cref="CompositionError"/> class
\r
84 /// with the specified error message, and composition element and exception that
\r
85 /// is the cause of the composition error.
\r
87 /// <param name="message">
\r
88 /// A <see cref="String"/> containing a message that describes the
\r
89 /// <see cref="CompositionError"/>; or <see langword="null"/> to set the
\r
90 /// <see cref="Description"/> property to an empty string ("").
\r
92 /// <param name="element">
\r
93 /// The <see cref="ICompositionElement"/> that is the cause of the
\r
94 /// <see cref="CompositionError"/>; or <see langword="null"/> to set
\r
95 /// the <see cref="CompositionError.Element"/> property to
\r
96 /// <see langword="null"/>.
\r
98 /// <param name="exception">
\r
99 /// The <see cref="Exception"/> that is the underlying cause of the
\r
100 /// <see cref="CompositionError"/>; or <see langword="null"/> to set
\r
101 /// the <see cref="CompositionError.Exception"/> property to <see langword="null"/>.
\r
103 public CompositionError(string message, ICompositionElement element, Exception exception)
\r
104 : this(CompositionErrorId.Unknown, message, element, exception)
\r
108 internal CompositionError(CompositionErrorId id, string description, ICompositionElement element, Exception exception)
\r
111 _description = description ?? string.Empty;
\r
112 _element = element;
\r
113 _exception = exception;
\r
117 /// Gets the composition element that is the cause of the error.
\r
120 /// The <see cref="ICompositionElement"/> that is the cause of the
\r
121 /// <see cref="CompositionError"/>. The default is <see langword="null"/>.
\r
123 public ICompositionElement Element
\r
125 get { return _element; }
\r
129 /// Gets the message that describes the composition error.
\r
132 /// A <see cref="String"/> containing a message that describes the
\r
133 /// <see cref="CompositionError"/>.
\r
135 public string Description
\r
137 get { return _description; }
\r
141 /// Gets the exception that is the underlying cause of the composition error.
\r
144 /// The <see cref="Exception"/> that is the underlying cause of the
\r
145 /// <see cref="CompositionError"/>. The default is <see langword="null"/>.
\r
147 public Exception Exception
\r
149 get { return _exception; }
\r
152 CompositionErrorId ICompositionError.Id
\r
154 get { return _id; }
\r
157 Exception ICompositionError.InnerException
\r
159 get { return Exception; }
\r
163 /// Returns a string representation of the composition error.
\r
166 /// A <see cref="String"/> containing the <see cref="Description"/> property.
\r
168 public override string ToString()
\r
170 return this.Description;
\r
173 internal static CompositionError Create(CompositionErrorId id, string format, params object[] parameters)
\r
175 return Create(id, (ICompositionElement)null, (Exception)null, format, parameters);
\r
178 internal static CompositionError Create(CompositionErrorId id, ICompositionElement element, string format, params object[] parameters)
\r
180 return Create(id, element, (Exception)null, format, parameters);
\r
183 internal static CompositionError Create(CompositionErrorId id, ICompositionElement element, Exception exception, string format, params object[] parameters)
\r
185 return new CompositionError(id, string.Format(CultureInfo.CurrentCulture, format, parameters), element, exception);
\r