update readme (#21797)
[mono-project.git] / mcs / class / System.Web / System.Web.UI / CollectionBuilder.cs
blob9f9a9f49528f765e08d596a3983cafc924221bc8
1 //
2 // System.Web.UI.CollectionBuilder.cs
3 //
4 // Authors:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System;
32 using System.Collections;
33 using System.Reflection;
34 using System.Text;
36 namespace System.Web.UI
38 sealed class CollectionBuilder : ControlBuilder
40 Type[] possibleElementTypes;
42 internal CollectionBuilder ()
46 public override void AppendLiteralString (string s)
48 if (s != null && s.Trim ().Length > 0)
49 throw new HttpException ("Literal content not allowed for " + ControlType);
52 public override Type GetChildControlType (string tagName, IDictionary attribs)
54 Type t = Root.GetChildControlType (tagName, attribs);
55 if (possibleElementTypes != null) {
56 bool foundMatchingType = false;
57 for (int i = 0; i < possibleElementTypes.Length && !foundMatchingType; ++i)
58 foundMatchingType = possibleElementTypes[i].IsAssignableFrom (t);
60 if (!foundMatchingType) {
61 StringBuilder possibleTypesString = new StringBuilder ();
62 for (int i = 0; i < possibleElementTypes.Length; i++) {
63 if (i != 0)
64 possibleTypesString.Append (", ");
65 possibleTypesString.Append (possibleElementTypes[i]);
67 throw new HttpException ("Cannot add a " + t + " to " + possibleTypesString);
71 return t;
74 public override void Init (TemplateParser parser,
75 ControlBuilder parentBuilder,
76 Type type,
77 string tagName,
78 string id,
79 IDictionary attribs)
81 base.Init (parser, parentBuilder, type, tagName, id, attribs);
83 PropertyInfo prop = parentBuilder.ControlType.GetProperty (tagName, FlagsNoCase);
84 SetControlType (prop.PropertyType);
86 MemberInfo[] mems = ControlType.GetMember ("Item", MemberTypes.Property, FlagsNoCase & ~BindingFlags.IgnoreCase);
87 if (mems.Length > 0) {
88 possibleElementTypes = new Type [mems.Length];
89 for (int i = 0; i < mems.Length; ++i)
90 possibleElementTypes [i] = ((PropertyInfo)mems [i]).PropertyType;
91 } else
92 throw new HttpException ("Collection of type '" + ControlType + "' does not have an indexer.");