FieldOrProperty.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // $Id$
  2. using System;
  3. using System.Reflection;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace LispReader
  7. {
  8. // TODO: Someone should try to understand what this is for and document it
  9. // It is probably some kind of simplifier of accessing property and field
  10. // metadata.
  11. public abstract class FieldOrProperty
  12. {
  13. private class FieldOrPropertyLister : IEnumerable<FieldOrProperty>
  14. {
  15. private Type type;
  16. public FieldOrPropertyLister(Type type)
  17. {
  18. this.type = type;
  19. }
  20. private IEnumerator<FieldOrProperty> GetEnumerator()
  21. {
  22. foreach(FieldInfo field in type.GetFields()) {
  23. yield return new Field(field);
  24. }
  25. foreach(PropertyInfo property in type.GetProperties()) {
  26. yield return new Property(property);
  27. }
  28. }
  29. IEnumerator<FieldOrProperty> IEnumerable<FieldOrProperty>.GetEnumerator()
  30. {
  31. return GetEnumerator();
  32. }
  33. IEnumerator IEnumerable.GetEnumerator()
  34. {
  35. return GetEnumerator();
  36. }
  37. }
  38. public static IEnumerable<FieldOrProperty> GetFieldsAndProperties(Type type)
  39. {
  40. return new FieldOrPropertyLister(type);
  41. }
  42. public abstract string Name {
  43. get;
  44. }
  45. public abstract Type Type {
  46. get;
  47. }
  48. public abstract void SetValue(object Object, object Value);
  49. public abstract object GetValue(object Object);
  50. public abstract object GetCustomAttribute(Type attributeType);
  51. public abstract object[] GetCustomAttributes(Type attributeType);
  52. private class Field : FieldOrProperty{
  53. private FieldInfo field;
  54. public Field(FieldInfo field)
  55. {
  56. this.field = field;
  57. }
  58. public override string Name {
  59. get {
  60. return field.Name;
  61. }
  62. }
  63. public override Type Type {
  64. get {
  65. return field.FieldType;
  66. }
  67. }
  68. public override void SetValue(object Object, object value)
  69. {
  70. field.SetValue(Object, value);
  71. }
  72. public override object GetValue(object Object)
  73. {
  74. return field.GetValue(Object);
  75. }
  76. public override object GetCustomAttribute(Type attributeType)
  77. {
  78. return Attribute.GetCustomAttribute(field, attributeType);
  79. }
  80. public override object[] GetCustomAttributes(Type attributeType)
  81. {
  82. return Attribute.GetCustomAttributes(field, attributeType);
  83. }
  84. }
  85. private class Property : FieldOrProperty{
  86. private PropertyInfo property;
  87. public Property(PropertyInfo property)
  88. {
  89. this.property = property;
  90. }
  91. public override string Name {
  92. get {
  93. return property.Name;
  94. }
  95. }
  96. public override Type Type {
  97. get {
  98. return property.PropertyType;
  99. }
  100. }
  101. public override void SetValue(object Object, object value)
  102. {
  103. property.SetValue(Object, value, null);
  104. }
  105. public override object GetValue(object Object)
  106. {
  107. return property.GetValue(Object, null);
  108. }
  109. public override object GetCustomAttribute(Type attributeType)
  110. {
  111. return Attribute.GetCustomAttribute(property, attributeType);
  112. }
  113. public override object[] GetCustomAttributes(Type attributeType)
  114. {
  115. return Attribute.GetCustomAttributes(property, attributeType);
  116. }
  117. }
  118. }
  119. }