PageRenderTime 60ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Utils/Reflection/PropertyChain.cs

https://github.com/dotnetchris/fluent-nhibernate
C# | 107 lines | 87 code | 20 blank | 0 comment | 7 complexity | a669d6823172ad49ad21b5b1bcd08b14 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using FluentNHibernate.Mapping;
  6. namespace FluentNHibernate.Utils
  7. {
  8. public class PropertyChain : Accessor
  9. {
  10. private readonly PropertyInfo[] _chain;
  11. private readonly SingleProperty _innerProperty;
  12. public PropertyChain(PropertyInfo[] properties)
  13. {
  14. _chain = new PropertyInfo[properties.Length - 1];
  15. for (int i = 0; i < _chain.Length; i++)
  16. {
  17. _chain[i] = properties[i];
  18. }
  19. _innerProperty = new SingleProperty(properties[properties.Length - 1]);
  20. }
  21. #region Accessor Members
  22. public void SetValue(object target, object propertyValue)
  23. {
  24. target = findInnerMostTarget(target);
  25. if (target == null)
  26. {
  27. return;
  28. }
  29. _innerProperty.SetValue(target, propertyValue);
  30. }
  31. public object GetValue(object target)
  32. {
  33. target = findInnerMostTarget(target);
  34. if (target == null)
  35. {
  36. return null;
  37. }
  38. return _innerProperty.GetValue(target);
  39. }
  40. public string FieldName
  41. {
  42. get { return _innerProperty.FieldName; }
  43. }
  44. public Type PropertyType
  45. {
  46. get { return _innerProperty.PropertyType; }
  47. }
  48. public PropertyInfo InnerProperty
  49. {
  50. get { return _innerProperty.InnerProperty; }
  51. }
  52. public Accessor GetChildAccessor<T>(Expression<Func<T, object>> expression)
  53. {
  54. PropertyInfo property = ReflectionHelper.GetProperty(expression);
  55. var list = new List<PropertyInfo>(_chain);
  56. list.Add(_innerProperty.InnerProperty);
  57. list.Add(property);
  58. return new PropertyChain(list.ToArray());
  59. }
  60. public string Name
  61. {
  62. get
  63. {
  64. string returnValue = string.Empty;
  65. foreach (var info in _chain)
  66. {
  67. returnValue += info.Name;
  68. }
  69. returnValue += _innerProperty.Name;
  70. return returnValue;
  71. }
  72. }
  73. #endregion
  74. private object findInnerMostTarget(object target)
  75. {
  76. foreach (PropertyInfo info in _chain)
  77. {
  78. target = info.GetValue(target, null);
  79. if (target == null)
  80. {
  81. return null;
  82. }
  83. }
  84. return target;
  85. }
  86. }
  87. }