fluent-nhibernate /src/FluentNHibernate/Utils/Reflection/PropertyChain.cs

Language C# Lines 107
MD5 Hash a669d6823172ad49ad21b5b1bcd08b14
Repository https://github.com/dotnetchris/fluent-nhibernate.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using FluentNHibernate.Mapping;

namespace FluentNHibernate.Utils
{
    public class PropertyChain : Accessor
    {
        private readonly PropertyInfo[] _chain;
        private readonly SingleProperty _innerProperty;

        public PropertyChain(PropertyInfo[] properties)
        {
            _chain = new PropertyInfo[properties.Length - 1];
            for (int i = 0; i < _chain.Length; i++)
            {
                _chain[i] = properties[i];
            }

            _innerProperty = new SingleProperty(properties[properties.Length - 1]);
        }

        #region Accessor Members

        public void SetValue(object target, object propertyValue)
        {
            target = findInnerMostTarget(target);
            if (target == null)
            {
                return;
            }

            _innerProperty.SetValue(target, propertyValue);
        }

        public object GetValue(object target)
        {
            target = findInnerMostTarget(target);

            if (target == null)
            {
                return null;
            }

            return _innerProperty.GetValue(target);
        }

        public string FieldName
        {
            get { return _innerProperty.FieldName; }
        }

        public Type PropertyType
        {
            get { return _innerProperty.PropertyType; }
        }

        public PropertyInfo InnerProperty
        {
            get { return _innerProperty.InnerProperty; }
        }

        public Accessor GetChildAccessor<T>(Expression<Func<T, object>> expression)
        {
            PropertyInfo property = ReflectionHelper.GetProperty(expression);
            var list = new List<PropertyInfo>(_chain);
            list.Add(_innerProperty.InnerProperty);
            list.Add(property);

            return new PropertyChain(list.ToArray());
        }

        public string Name
        {
            get
            {
                string returnValue = string.Empty;
                foreach (var info in _chain)
                {
                    returnValue += info.Name;
                }

                returnValue += _innerProperty.Name;

                return returnValue;
            }
        }

        #endregion

        private object findInnerMostTarget(object target)
        {
            foreach (PropertyInfo info in _chain)
            {
                target = info.GetValue(target, null);
                if (target == null)
                {
                    return null;
                }
            }

            return target;
        }
    }
}
Back to Top