TestDataColumn.cs 1.8 KB

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