예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <param name="elementValue"></param>
        /// <param name="property"></param>
        /// <param name="converter"></param>
        /// <returns></returns>
        private static T ResolveValue <T>(object item, T elementValue, string property, ITemplateConverter <T> converter)
        {
            if (String.IsNullOrEmpty(property) && (converter == null))
            {
                return(elementValue);
            }

            var value = item;

            if ((value != null) && !String.IsNullOrEmpty(property) && (property != "."))
            {
                var accessor = TemplateAccessorCache.GetAccessor(item.GetType(), property);
                if ((accessor != null) && accessor.CanRead)
                {
                    value = accessor.GetValue(value);
                }
            }

            if (converter != null)
            {
                value = converter.Convert(value);
            }

            try
            {
                return(value is T ? (T)value : (T)Convert.ChangeType(value, typeof(T), null));
            }
            catch (InvalidCastException)
            {
                return(default(T));
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public string Convert(object source)
        {
            if (source == null)
            {
                return(string.Empty);
            }

            var type = source.GetType();

            var sb = new StringBuilder();

            foreach (var accessor in Properties.Split(',').Select(x => TemplateAccessorCache.GetAccessor(type, x.Trim())).Where(x => (x != null) && x.CanRead))
            {
                sb.Append(accessor.GetValue(source));
            }

            return(sb.ToString());
        }