Example #1
0
        /// <summary>
        /// Invokes the getter property chain.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="propertyChain">The property chain.</param>
        /// <returns></returns>
        public static dynamic InvokeGetChain(object target, string propertyChain)
        {
            var tProperties = _chainRegex.FluentMatches(propertyChain);
            var tTarget     = target;

            foreach (var tProperty in tProperties)
            {
                var tGetter        = tProperty.Getter;
                var tIntIndexer    = tProperty.IntIndexer;
                var tStringIndexer = tProperty.StringIndexer;

                if (tGetter != null)
                {
                    tTarget = InvokeGet(tTarget, tGetter);
                }
                else if (tIntIndexer != null)
                {
                    tTarget = InvokeGetIndex(tTarget, Impromptu.CoerceConvert(tIntIndexer, typeof(int)));
                }
                else if (tStringIndexer != null)
                {
                    tTarget = InvokeGetIndex(tTarget, tStringIndexer);
                }
                else
                {
                    throw new Exception(string.Format("Could Not Parse :'{0}'", propertyChain));
                }
            }
            return(tTarget);
        }
Example #2
0
        /// <summary>
        /// Invokes the set on the end of a property chain.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="propertyChain">The property chain.</param>
        /// <param name="value">The value.</param>
        public static object InvokeSetChain(object target, string propertyChain, object value)
        {
            var tProperties    = _chainRegex.FluentMatches(propertyChain).ToList();
            var tGetProperties = tProperties.Take(tProperties.Count - 1);


            var tTarget = target;

            foreach (var tProperty in tGetProperties)
            {
                var tGetter        = tProperty.Getter;
                var tIntIndexer    = tProperty.IntIndexer;
                var tStringIndexer = tProperty.StringIndexer;

                if (tGetter != null)
                {
                    tTarget = InvokeGet(tTarget, tGetter);
                }
                else if (tIntIndexer != null)
                {
                    tTarget = InvokeGetIndex(tTarget, Impromptu.CoerceConvert(tIntIndexer, typeof(int)));
                }
                else if (tStringIndexer != null)
                {
                    tTarget = InvokeGetIndex(tTarget, tStringIndexer);
                }
                else
                {
                    throw new Exception(string.Format("Could Not Parse :'{0}'", propertyChain));
                }
            }

            var tSetProperty = tProperties.Last();

            var tSetGetter        = tSetProperty.Getter;
            var tSetIntIndexer    = tSetProperty.IntIndexer;
            var tSetStringIndexer = tSetProperty.StringIndexer;

            if (tSetGetter != null)
            {
                return(InvokeSet(tTarget, tSetGetter, value));
            }
            if (tSetIntIndexer != null)
            {
                return(InvokeSetIndex(tTarget, Impromptu.CoerceConvert(tSetIntIndexer, typeof(int)), value));
            }
            if (tSetStringIndexer != null)
            {
                return(InvokeSetIndex(tTarget, tSetStringIndexer, value));
            }

            throw new Exception(string.Format("Could Not Parse :'{0}'", propertyChain));
        }