**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / System.Data / DataColumnPropertyDescriptor.cs
blob11aa8d70a779590694080770c9209e3685714eef
1 //
2 // System.Data.DataColumnPropertyDescriptor.cs
3 //
4 // Author:
5 // Daniel Morgan <danmorg@sc.rr.com>
6 //
7 // (c) copyright 2002 Daniel Morgan
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Data.Common;
35 using System.ComponentModel;
36 using System.Reflection;
38 namespace System.Data
40 internal class DataColumnPropertyDescriptor : PropertyDescriptor
42 private bool readOnly = true;
43 private Type componentType = null;
44 private Type propertyType = null;
45 private PropertyInfo prop = null;
46 private int columnIndex = 0;
48 public DataColumnPropertyDescriptor (string name, int columnIndex, Attribute [] attrs)
49 : base (name, attrs)
51 this.columnIndex = columnIndex;
54 public void SetReadOnly (bool value)
56 readOnly = value;
59 public void SetComponentType (Type type)
61 componentType = type;
64 public void SetPropertyType (Type type)
66 propertyType = type;
69 private PropertyInfo GetPropertyInfo ()
71 string defaultMemberName = "";
72 object[] attribs = componentType.GetCustomAttributes (true);
74 for (int at = 0; at < attribs.Length; at++) {
75 if (attribs[at] is DefaultMemberAttribute) {
76 defaultMemberName = ((DefaultMemberAttribute) attribs[at]).MemberName;
77 break;
81 // FIXME: what do I do if a DefaultMemeberAttribute is not found?
82 // should I try looking for DefaultPropertyAttribute?
83 if (defaultMemberName.Equals(""))
84 throw new Exception("Default property not found.");
86 Type[] parmTypes = new Type[1];
87 parmTypes[0] = propertyType;
88 PropertyInfo propertyInfo = componentType.GetProperty (defaultMemberName, parmTypes);
89 return propertyInfo;
92 public override object GetValue (object component)
94 // FIXME: what is the correct way to Get a Value?
95 if(componentType == typeof(DataRowView) && component is DataRowView) {
96 DataRowView drv = (DataRowView) component;
97 return drv[base.Name];
99 else if(componentType == typeof(DbDataRecord) && component is DbDataRecord) {
100 DbDataRecord dr = (DbDataRecord) component;
101 return dr[columnIndex];
103 throw new InvalidOperationException();
106 if (prop == null)
107 prop = GetPropertyInfo ();
109 // FIXME: should I allow multiple parameters?
110 object[] parms = new object[1];
111 parms[0] = base.Name;
112 return prop.GetValue (component, parms);
116 public override void SetValue(object component, object value)
118 DataRowView drv = (DataRowView) component;
119 drv[base.Name] = value;
121 if (prop == null)
122 prop = GetPropertyInfo ();
124 if (readOnly == true) {
125 // FIXME: what really happens if read only?
126 throw new Exception("Property is ReadOnly");
129 // FIXME: should I allow multiple parameters?
130 object[] parms = new Object[1];
131 parms[0] = base.Name;
132 prop.SetValue (component, value, parms);
136 [MonoTODO]
137 public override void ResetValue(object component)
139 // FIXME:
142 [MonoTODO]
143 public override bool CanResetValue(object component)
145 return false; // FIXEME
148 [MonoTODO]
149 public override bool ShouldSerializeValue(object component)
151 return false;
154 public override Type ComponentType {
155 get {
156 return componentType;
160 public override bool IsReadOnly {
161 get {
162 return readOnly;
166 public override Type PropertyType {
167 get {
168 return propertyType;