1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
22 /// Helper used to create <see cref="IDictionary"/> instances
24 public class DictHelper
: AbstractHelper
27 /// Creates an <see cref="IDictionary"/> with entries
28 /// infered from the arguments.
30 /// CreateDict( "style=display: none;", "selected" )
33 /// <param name="args"></param>
34 /// <returns></returns>
35 public IDictionary
CreateDict(params String
[] args
)
41 /// Creates a dictionary from specified arguments.
43 /// <param name="args">The arguments.</param>
44 /// <returns></returns>
45 public static IDictionary
Create(params String
[] args
)
47 IDictionary dict
= new HybridDictionary(true);
49 foreach(String arg
in args
)
51 String
[] parts
= arg
.Split('=');
53 if (parts
.Length
== 1)
57 else if (parts
.Length
== 2)
59 dict
[ parts
[0] ] = parts
[1];
63 dict
[ parts
[0] ] = String
.Join("=", parts
, 1, parts
.Length
- 1);
71 /// Creates a dictionary fros a name value collection.
73 /// <param name="collection">The collection.</param>
74 /// <returns></returns>
75 public IDictionary
FromNameValueCollection(NameValueCollection collection
)
77 IDictionary dict
= new HybridDictionary(true);
79 foreach(string key
in collection
.AllKeys
)
81 if (key
== null) continue;
82 dict
[key
] = collection
.GetValues(key
);