Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / DictHelper.cs
blobc49eab3f8940c52231adbc81e854270633a83827
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Helpers
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
21 /// <summary>
22 /// Helper used to create <see cref="IDictionary"/> instances
23 /// </summary>
24 public class DictHelper : AbstractHelper
26 #region MonoRailDictionary inner class
27 /// <summary>
28 /// Helper (for the Helper) used to create <see cref="IDictionary"/> instances.
29 ///
30 /// </summary>
31 public class MonoRailDictionary : HybridDictionary
33 /// <summary>
34 /// Initializes a new instance of the <see cref="MonoRailDictionary"/> class.
35 /// Forces case Insensitivty.
36 /// </summary>
37 public MonoRailDictionary() : base(true) { }
40 // Removed. See explaination in outer class.
41 //public MonoRailDictionary N<T>(string key, T value)
42 //{
43 // this[key] = value.ToString();
44 // return (this);
45 //}
47 /// <summary>
48 /// Adds the specified key &amp; value to the collection.
49 /// </summary>
50 /// <remarks>Usuable in placed where the generic version is not available.</remarks>
51 /// <param name="key">The key.</param>
52 /// <param name="value">The value.</param>
53 /// <returns>itself, to allow for chaning.</returns>
54 public MonoRailDictionary N(string key, object value)
56 this[key] = value.ToString();
57 return (this);
60 /// <summary>
61 /// Adds the specified key to the collection.
62 /// </summary>
63 /// <param name="key">The key.</param>
64 /// <returns>itself, to allow for chaning.</returns>
65 public MonoRailDictionary N(string key)
67 this[key] = "";
68 return (this);
71 #endregion
73 #region Constructors
74 /// <summary>
75 /// Initializes a new instance of the <see cref="DictHelper"/> class.
76 /// </summary>
77 public DictHelper() { }
78 /// <summary>
79 /// Initializes a new instance of the <see cref="DictHelper"/> class.
80 /// </summary>
81 /// <param name="engineContext">The engine context.</param>
82 public DictHelper(IEngineContext engineContext) : base(engineContext) { }
83 #endregion
85 /// <summary>
86 /// Creates an <see cref="IDictionary"/> with entries
87 /// infered from the arguments.
88 /// </summary>
89 /// <example>
90 /// <code>
91 /// helper.CreateDict( "style=display: none;", "selected" )
92 /// </code></example>
93 /// <param name="args"></param>
94 /// <returns>an <see cref="IDictionary"/></returns>
95 public MonoRailDictionary CreateDict(params String[] args)
97 return Create(args);
100 /// <summary>
101 /// Creates a dictionary from specified arguments.
102 /// </summary>
103 /// <remarks> Returns a MonoRailDictionary object,
104 /// which is an IDictionary like previous versions of this method.
105 /// </remarks>
106 /// <example>
107 /// <code>
108 /// DictHelper.Create( "style=display: none;", "selected" )
109 /// </code></example>
110 /// <param name="args">The arguments.</param>
111 /// <returns>an <see cref="IDictionary"/></returns>
112 public static MonoRailDictionary Create(params String[] args)
114 MonoRailDictionary dict = new MonoRailDictionary();
115 foreach (String arg in args)
117 int pos = arg.IndexOf('=');
119 if (pos == -1)
121 dict[arg] = "";
123 else
125 dict[arg.Substring(0, pos)] = arg.Substring(pos + 1);
128 return dict;
131 ///// <summary>
132 ///// Create a new collections and adds the specified key &amp; value.
133 ///// </summary>
134 ///// <typeparam name="T"></typeparam>
135 ///// <param name="key">The key.</param>
136 ///// <param name="value">The value.</param>
137 ///// <returns></returns>
139 // Generic version can't be used in an nVelocity template,
140 // and the non-generic, object version (below) would need a
141 // different name to avoid conflict, so I figured I'd
142 // just get rid of it, even thought it's slightly faster.
143 //public MonoRailDictionary N<T>(string key, T value)
145 // MonoRailDictionary helper = new MonoRailDictionary();
146 // helper[key] = value.ToString();
147 // return helper;
150 /// <summary>
151 /// Creates an <see cref="IDictionary"/> and adds the
152 /// specified key &amp; value to the collection.
153 /// </summary>
154 /// <param name="name">The name.</param>
155 /// <param name="value">The value.</param>
156 /// <returns>an <see cref="IDictionary"/></returns>
157 public MonoRailDictionary N(string name, object value)
159 return CreateN(name, value);
162 /// <summary>
163 /// Creates an <see cref="IDictionary"/> and adds the
164 /// specified key &amp; value to the collection.
165 /// </summary>
166 /// <param name="key">The key.</param>
167 /// <param name="value">The value.</param>
168 /// <returns>an <see cref="IDictionary"/></returns>
169 public static MonoRailDictionary CreateN(string key, object value)
171 MonoRailDictionary helper = new MonoRailDictionary();
172 helper[key] = value.ToString();
173 return helper;
176 /// <summary>
177 /// Creates a dictionary froms a name value collection.
178 /// </summary>
179 /// <param name="collection">The collection.</param>
180 /// <returns></returns>
181 public IDictionary FromNameValueCollection(NameValueCollection collection)
183 IDictionary dict = new MonoRailDictionary();
185 foreach(string key in collection.AllKeys)
187 if (key != null)
189 dict[key] = collection.GetValues(key);
193 return dict;