[corlib] Avoid unnecessary ephemeron array resizes
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Metadata / AttributeTableValidationException.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Metadata 
6 {
7
8     using System;
9     using System.Collections.Generic;
10     using System.Runtime.Serialization;
11     using System.Security.Permissions;
12     using System.Activities.Presentation;
13
14     // <summary>
15     // This exception may be thrown from the ValidateTable method on
16     // AttributeTable.  It will be thrown if the metadata provided in
17     // the table does not match properties, methods and events on real
18     // types.
19     // </summary>
20     [Serializable]
21     public class AttributeTableValidationException : Exception 
22     {
23         private string[] _validationErrors;
24
25         // <summary>
26         // Creates a new AttributeTableValidationException.
27         // </summary>
28         public AttributeTableValidationException() : base() 
29         {
30         }
31
32         // <summary>
33         // Creates a new AttributeTableValidationException.
34         // </summary>
35         // <param name="message"></param>
36         public AttributeTableValidationException(string message)
37             : base(message) 
38         {
39         }
40
41         // <summary>
42         // Creates a new AttributeTableValidationException.
43         // </summary>
44         // <param name="message">The message provided to the user.</param>
45         // <param name="inner">An optional inner exception.</param>
46         public AttributeTableValidationException(string message, Exception inner)
47             : base(message, inner) 
48         {
49         }
50
51         // <summary>
52         // Creates a new AttributeTableValidationException.
53         // </summary>
54         // <param name="message">The message provided to the user.</param>
55         // <param name="validationErrors">Zero or more errors that occurred during validation.</param>
56         public AttributeTableValidationException(string message, IEnumerable<string> validationErrors)
57             : base(message) 
58         {
59             _validationErrors = CreateArray(validationErrors);
60         }
61
62         // <summary>
63         // Creates a new AttributeTableValidationException.
64         // </summary>
65         // <param name="message">The message provided to the user.</param>
66         // <param name="inner">An optional inner exception.</param>
67         // <param name="validationErrors">Zero or more errors that occurred during validation.</param>
68         public AttributeTableValidationException(string message, Exception inner, IEnumerable<string> validationErrors)
69             : base(message, inner) 
70         {
71             _validationErrors = CreateArray(validationErrors);
72         }
73
74         // <summary>
75         // Used during serialization to deserialize an exception.
76         // </summary>
77         // <param name="info">The serialization store.</param>
78         // <param name="context">The serialization context.</param>
79         protected AttributeTableValidationException(SerializationInfo info, StreamingContext context)
80             : base(info, context) 
81         {
82             if (info == null) 
83             {
84                 throw FxTrace.Exception.ArgumentNull("info");
85             }
86             _validationErrors = (string[])info.GetValue("ValidationErrors", typeof(string[]));
87         }
88
89         // <summary>
90         // Returns an enumeration of validation errors.
91         // </summary>
92         public IEnumerable<string> ValidationErrors 
93         {
94             get { return _validationErrors; }
95         }
96
97         //
98         // Helper method to create an array from an enumeration.
99         //
100         private static string[] CreateArray(IEnumerable<string> validationErrors) {
101
102             string[] array;
103
104             if (validationErrors != null) 
105             {
106                 int cnt = 0;
107                 IEnumerator<string> e = validationErrors.GetEnumerator();
108                 while (e.MoveNext()) 
109                 {
110                     cnt++;
111                 }
112
113                 e.Reset();
114
115                 array = new string[cnt];
116
117                 cnt = 0;
118
119                 while (e.MoveNext()) 
120                 {
121                     array[cnt++] = e.Current;
122                 }
123             }
124             else 
125             {
126                 array = new string[0];
127             }
128
129             return array;
130         }
131
132         // <summary>
133         // Override of Exception's GetObjectData that is used to perform serialization.
134         // </summary>
135         // <param name="info">The serialization store.</param>
136         // <param name="context">The serialization context.</param>
137         public override void GetObjectData(SerializationInfo info, StreamingContext context) 
138         {
139             if (info == null) 
140             {
141                 throw FxTrace.Exception.ArgumentNull("info");
142             }
143             base.GetObjectData(info, context);
144             info.AddValue("ValidationErrors", _validationErrors);
145         }
146     }
147 }