2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / TestDataColumn.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 using System.Text;
6
7 using MonoTests.DataSource;
8 using MonoTests.ModelProviders;
9
10 namespace MonoTests.Common
11 {
12         public class TestDataColumn<DataType> : DynamicDataColumn
13         {
14                 public TestDataColumn (MemberInfo member)
15                 {
16                         if (member == null)
17                                 throw new ArgumentNullException ("member");
18
19                         string name = this.Name = member.Name;
20                         switch (member.MemberType) {
21                                 case MemberTypes.Field:
22                                         var fi = member as FieldInfo;
23                                         this.DataType = fi.FieldType;
24                                         break;
25
26                                 case MemberTypes.Property:
27                                         var pi = member as PropertyInfo;
28                                         this.DataType = pi.PropertyType;
29                                         break;
30
31                                 default:
32                                         throw new ArgumentException ("Member information must refer to either a field or a property.", "member");
33                         }
34
35                         this.PrimaryKey = name.StartsWith ("PrimaryKeyColumn", StringComparison.Ordinal);
36                         this.CustomProperty = name.StartsWith ("CustomProperty", StringComparison.Ordinal);
37                         this.Generated = name.StartsWith ("GeneratedColumn", StringComparison.Ordinal);
38                         
39                         object[] attrs = member.GetCustomAttributes (true);
40                         DynamicDataAssociationAttribute associationAttr;
41
42                         try {
43                                 associationAttr = attrs.OfType<DynamicDataAssociationAttribute> ().First<DynamicDataAssociationAttribute> ();
44                         } catch (InvalidOperationException) {
45                                 associationAttr = null;
46                         }
47
48                         if (associationAttr != null) {
49                                 AssociatedTo = associationAttr.ColumnName;
50                                 AssociationDirection = associationAttr.Direction;
51                         }
52
53                         DynamicDataSortableAttribute sortableAttr;
54
55                         try {
56                                 sortableAttr = attrs.OfType<DynamicDataSortableAttribute> ().First<DynamicDataSortableAttribute> ();
57                         } catch (InvalidOperationException) {
58                                 sortableAttr = null;
59                         }
60
61                         if (sortableAttr != null)
62                                 Sortable = sortableAttr.Sortable;
63                 }
64         }
65 }