Exemplo n.º 1
0
        /// <summary>
        /// Provided to fomat string using the razor format.
        /// </summary>
        /// <param name="format">string format</param>
        /// <param name="obj">the instance.</param>
        /// <returns>the converted string.</returns>
        public static string RazorFormat(this string format, object obj)
        {
            ThrowExceptionUtil.ArgumentNotNull(format, "format");
            ThrowExceptionUtil.ArgumentNotNull(obj, "obj");
            string result = string.Empty;

            List <string> propertyList = new List <string>();
            Match         match        = FormatRegex.Match(format);

            while (match.Success)
            {
                propertyList.Add(match.Groups["name"].Value);
                match = match.NextMatch();
            }

            result = format;

            if (obj is DataRow)
            {
                DataRow rowItem = obj as DataRow;
                foreach (string item in propertyList)
                {
                    if (rowItem.Table.Columns.Contains(item))
                    {
                        result = result.Replace("@" + item, rowItem[item].ToString());
                    }
                }
            }
            else if (obj is BeeDataAdapter)
            {
                BeeDataAdapter dataAdapter = obj as BeeDataAdapter;
                foreach (string item in propertyList)
                {
                    result = result.Replace("@" + item, dataAdapter.Format(item));
                }
            }
            else
            {
                IEntityProxy entityProxy = EntityProxyManager.Instance.GetEntityProxyFromType(obj.GetType());
                foreach (string item in propertyList)
                {
                    object propertyValue = entityProxy.GetPropertyValue(obj, item);
                    result = result.Replace("@" + item, propertyValue == null ? string.Empty : propertyValue.ToString());
                }
            }

            return(result);
        }