[bcl] Remove NET_4_0 defines from class libs.
[mono.git] / mcs / class / WebMatrix.Data / WebMatrix.Data / DynamicRecord.cs
1 //
2 // DynamicRecord.cs
3 //
4 // Copyright (c) 2011 Novell
5 //
6 // Authors:
7 //     Jérémie "garuma" Laval
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 //
27
28
29 using System;
30 using System.Dynamic;
31 using System.Data.Common;
32 using System.Collections.Generic;
33 using System.ComponentModel;
34
35 namespace WebMatrix.Data
36 {
37         public sealed class DynamicRecord : DynamicObject, ICustomTypeDescriptor
38         {
39                 readonly Dictionary<string, object> fields;
40
41                 internal DynamicRecord (Dictionary<string, object> fields)
42                 {
43                         this.fields = fields;
44                         Columns = new List<string> (fields.Keys).AsReadOnly ();
45                 }
46
47                 public IList<string> Columns {
48                         get;
49                         private set;
50                 }
51
52                 public object this[string name] {
53                         get {
54                                 var retval = fields[name];
55
56                                 if (retval == DBNull.Value)
57                                         return null;
58
59                                 return retval;
60                         }
61                 }
62
63                 public object this[int index] {
64                         get {                           
65                                 var retval = fields[Columns[index]];
66
67                                 if (retval == DBNull.Value)
68                                         return null;
69
70                                 return retval;
71                         }
72                 }
73
74                 public override IEnumerable<string> GetDynamicMemberNames ()
75                 {
76                         return fields.Keys;
77                 }
78
79                 public override bool TryGetMember (GetMemberBinder binder, out object result)
80                 {
81                         bool success = fields.TryGetValue (binder.Name, out result);
82
83                         if (result == DBNull.Value)
84                                 result = null;
85
86                         return success;
87                 }
88
89                 AttributeCollection ICustomTypeDescriptor.GetAttributes ()
90                 {
91                         return null;
92                 }
93
94                 string ICustomTypeDescriptor.GetClassName ()
95                 {
96                         return null;
97                 }
98
99                 string ICustomTypeDescriptor.GetComponentName ()
100                 {
101                         return null;
102                 }
103
104                 TypeConverter ICustomTypeDescriptor.GetConverter ()
105                 {
106                         return null;
107                 }
108
109                 EventDescriptor ICustomTypeDescriptor.GetDefaultEvent ()
110                 {
111                         return null;
112                 }
113
114                 PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty ()
115                 {
116                         return null;
117                 }
118
119                 Object ICustomTypeDescriptor.GetEditor (Type editorBaseType)
120                 {
121                         return null;
122                 }
123
124                 Object ICustomTypeDescriptor.GetPropertyOwner (PropertyDescriptor pd)
125                 {
126                         return null;
127                 }
128
129                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents ()
130                 {
131                         return null;
132                 }
133
134                 EventDescriptorCollection ICustomTypeDescriptor.GetEvents (Attribute[] attributes)
135                 {
136                         return null;
137                 }
138
139                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties ()
140                 {
141                         return null;
142                 }
143
144                 PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties (Attribute[] attributes)
145                 {
146                         return null;
147                 }
148         }
149 }
150