Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ModelBinders.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.ComponentModel;\r
16     using System.Data.Linq;\r
17     using System.Linq;\r
18     using System.Reflection;\r
19     using System.Web;\r
20 \r
21     public static class ModelBinders {\r
22 \r
23         private static readonly ModelBinderDictionary _binders = CreateDefaultBinderDictionary();\r
24 \r
25         public static ModelBinderDictionary Binders {\r
26             get {\r
27                 return _binders;\r
28             }\r
29         }\r
30 \r
31         internal static IModelBinder GetBinderFromAttributes(Type type, Func<string> errorMessageAccessor) {\r
32             AttributeCollection allAttrs = TypeDescriptorHelper.Get(type).GetAttributes();\r
33             CustomModelBinderAttribute[] filteredAttrs = allAttrs.OfType<CustomModelBinderAttribute>().ToArray();\r
34             return GetBinderFromAttributesImpl(filteredAttrs, errorMessageAccessor);\r
35         }\r
36 \r
37         internal static IModelBinder GetBinderFromAttributes(ICustomAttributeProvider element, Func<string> errorMessageAccessor) {\r
38             CustomModelBinderAttribute[] attrs = (CustomModelBinderAttribute[])element.GetCustomAttributes(typeof(CustomModelBinderAttribute), true /* inherit */);\r
39             return GetBinderFromAttributesImpl(attrs, errorMessageAccessor);\r
40         }\r
41 \r
42         private static IModelBinder GetBinderFromAttributesImpl(CustomModelBinderAttribute[] attrs, Func<string> errorMessageAccessor) {\r
43             // this method is used to get a custom binder based on the attributes of the element passed to it.\r
44             // it will return null if a binder cannot be detected based on the attributes alone.\r
45 \r
46             if (attrs == null) {\r
47                 return null;\r
48             }\r
49 \r
50             switch (attrs.Length) {\r
51                 case 0:\r
52                     return null;\r
53 \r
54                 case 1:\r
55                     IModelBinder binder = attrs[0].GetBinder();\r
56                     return binder;\r
57 \r
58                 default:\r
59                     string errorMessage = errorMessageAccessor();\r
60                     throw new InvalidOperationException(errorMessage);\r
61             }\r
62         }\r
63 \r
64         private static ModelBinderDictionary CreateDefaultBinderDictionary() {\r
65             // We can't add a binder to the HttpPostedFileBase type as an attribute, so we'll just\r
66             // prepopulate the dictionary as a convenience to users.\r
67 \r
68             ModelBinderDictionary binders = new ModelBinderDictionary() {\r
69                 { typeof(HttpPostedFileBase), new HttpPostedFileBaseModelBinder() },\r
70                 { typeof(byte[]), new ByteArrayModelBinder() },\r
71                 { typeof(Binary), new LinqBinaryModelBinder() }\r
72             };\r
73             return binders;\r
74         }\r
75 \r
76     }\r
77 }\r