Revert "PowerPC64 ELFv2 ABI: cases for in-register parameter passing, return values...
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / System.ComponentModel.DataAnnotations / ValidationContext.cs
1 //
2 // ValidationContext.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell Inc. (http://novell.com)
8 //
9
10 //
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.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.ComponentModel.Design;
33
34 namespace System.ComponentModel.DataAnnotations
35 {
36         public sealed class ValidationContext : IServiceProvider
37         {
38                 public string DisplayName { get; set; }
39                 public IDictionary <object, object> Items { get; private set; }
40                 public string MemberName { get; set; }
41                 public object ObjectInstance { get; private set; }
42                 public Type ObjectType { get; private set; }
43                 public IServiceContainer ServiceContainer { get; private set; }
44                 
45                 public ValidationContext (object instance, IServiceProvider serviceProvider, IDictionary<object, object> items)
46                 {
47                         if (instance == null)
48                                 throw new ArgumentNullException ("instance");
49                         
50                         ObjectInstance = instance;
51                         ObjectType = instance.GetType ();
52                         if (items != null)
53                                 Items = new Dictionary <object, object> (items);
54                         else
55                                 Items = new Dictionary <object, object> ();
56                         
57                         DisplayName = instance.GetType ().Name;
58
59                         // LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements 
60                         // the IServiceContainer interface - not the case, though.
61                         //
62                         // IServiceContainer container = serviceProvider as IServiceContainer;
63                         // if (container != null)
64                         //      ServiceContainer = container;
65                         // else
66                         ServiceContainer = new ValidationContextServiceContainer ();
67                 }
68                 
69 #if NET_4_5
70                 public ValidationContext (object instance)
71                         : this (instance, null, null)
72                 {
73                 }
74                 
75                 public ValidationContext (object instance, IDictionary<object, object> items)
76                         : this (instance, null, items)
77                 {
78                 }
79
80                 // FIXME: According to MSDN, this should be defined in
81                 //        4.5, Silverlight and PCL
82                 [MonoTODO]
83                 public void InitializeServiceProvider (
84                         Func<Type, Object> serviceProvider)
85                 {
86                         throw new NotImplementedException ();
87                 }
88 #endif
89                 
90                 public object GetService (Type serviceType)
91                 {
92                         return ServiceContainer.GetService (serviceType);
93                 }
94
95                 sealed class ValidationContextServiceContainer : IServiceContainer
96                 {
97                         Dictionary <Type, object> services = new Dictionary <Type, object> ();
98                         
99                         public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
100                         {
101                                 AddService (serviceType, (object)callback, promote);
102                         }
103
104                         public void AddService (Type serviceType, ServiceCreatorCallback callback)
105                         {
106                                 AddService (serviceType, callback, false);
107                         }
108
109                         public void AddService (Type serviceType, object serviceInstance, bool promote)
110                         {
111                                 if (serviceType == null)
112                                         throw new ArgumentNullException ("serviceType");
113                                 
114                                 if (services.ContainsKey (serviceType))
115                                         throw new ArgumentException (
116                                                 String.Format ("A service of type '{0}' already exists in the container.", serviceType)
117                                         );
118
119                                 services.Add (serviceType, serviceInstance);
120                         }
121
122                         public void AddService (Type serviceType, object serviceInstance)
123                         {
124                                 AddService (serviceType, serviceInstance, false);
125                         }
126
127                         public void RemoveService (Type serviceType, bool promote)
128                         {
129                                 if (serviceType == null)
130                                         throw new ArgumentNullException ("serviceType");
131                                 
132                                 if (!services.ContainsKey (serviceType))
133                                         return;
134
135                                 services.Remove (serviceType);
136                         }
137
138                         public void RemoveService (Type serviceType)
139                         {
140                                 RemoveService (serviceType, false);
141                         }
142
143                         public object GetService (Type serviceType)
144                         {
145                                 if (serviceType == null)
146                                         throw new ArgumentNullException ("serviceType");
147                                 
148                                 object o;
149                                 if (!services.TryGetValue (serviceType, out o))
150                                         return null;
151
152                                 var cb = o as ServiceCreatorCallback;
153                                 if (cb != null)
154                                         return cb (this, serviceType);
155
156                                 return o;
157                         }
158                 }
159         }
160 }