Ejemplo n.º 1
0
        public static void SetValue(object obj, string propertyName, object value)
        {
            if (propertyName.Contains("{"))
            {
                propertyName.Replace("{", "");
            }

            if (propertyName.Contains("}"))
            {
                propertyName.Replace("}", "");
            }

            var nestedProperties = StringHelper.SplitByString(propertyName, ".");

            if (nestedProperties.Length > 0)
            {
                object tempNestedProperty = obj;
                foreach (var nestedProperty in nestedProperties)
                {
                    var tempPropertyInfo = tempNestedProperty.GetType().GetProperty(nestedProperty);

                    if (tempPropertyInfo != null)
                    {
                        var val = tempPropertyInfo.GetValue(tempNestedProperty, null);

                        if (val is string || val is bool || val is long || val == null)
                        {
                            if (System.ComponentModel.TypeDescriptor.GetConverter(tempPropertyInfo.PropertyType).CanConvertFrom(value.GetType()))
                            {
                                if (value != "")
                                {
                                    try
                                    {
                                        tempPropertyInfo.SetValue(tempNestedProperty, Convert.ChangeType(value, tempPropertyInfo.PropertyType), null);
                                    }
                                    catch (Exception ex)
                                    {
                                        ErrorHelper.LogException(new Exception($"Error setting value for property '{tempPropertyInfo.Name}' with value '{value}' for control with ID '{((System.Web.UI.Control)obj).ClientID}'", ex));
                                    }
                                }
                            }
                        }
                        else
                        {
                            tempNestedProperty = val;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public List <Control> GetTemplateTopAndBottomSegments(System.Web.UI.Page control)
        {
            var masterPage = GetMasterPage();
            var templateTopAndBottomSegments = new List <Control>();

            if (masterPage != null)
            {
                var topAndBottomSegments = StringHelper.SplitByString(masterPage.Layout, "{PageContent}");

                if (topAndBottomSegments.Length > 1)
                {
                    templateTopAndBottomSegments.Add(control.ParseControl(MediaDetailsMapper.ParseSpecialTags(this, topAndBottomSegments.ElementAt(0))));
                    templateTopAndBottomSegments.Add(control.ParseControl(MediaDetailsMapper.ParseSpecialTags(this, topAndBottomSegments.ElementAt(1))));

                    return(templateTopAndBottomSegments);
                }
            }

            return(templateTopAndBottomSegments);
        }
Ejemplo n.º 3
0
        public static string ParseData(string data, object obj, bool compileRazor = true)
        {
            //return data;

            if (obj == null)
            {
                return("");
            }

            data = data.Trim();

            if (data.StartsWith("@") && data.EndsWith("}"))
            {
                data = RunOrCompileRazorCode(data, data, obj, compileRazor);
            }

            var matches = Regex.Matches(data, openToken + "[a-zA-Z0-9-.&=<>/\\;(\n|\r|\r\n)\"#?']+" + closeToken);

            foreach (var item in matches)
            {
                var tag               = item.ToString();
                var tagValue          = "";
                var propertyName      = tag.Replace("{", "").Replace("}", "");
                var queryStringParams = "";

                if (propertyName.Contains("?"))
                {
                    var segments = propertyName.Split('?').ToList();
                    propertyName = segments[0];
                    segments.RemoveAt(0);
                    queryStringParams = string.Join("", segments);
                }

                var nestedProperties = StringHelper.SplitByString(propertyName, ".");

                if (nestedProperties.Length > 0)
                {
                    if (obj == null)
                    {
                        continue;
                    }

                    object tempNestedProperty = obj;
                    var    propertyloopIndex  = 0;

                    foreach (var nestedProperty in nestedProperties)
                    {
                        PropertyInfo tempPropertyInfo = null;
                        MethodInfo   tempMethodInfo   = null;

                        var matchParams = Regex.Matches(nestedProperty, "([a-zA-Z0-9-_]+)");

                        var methodParamsMatches = new List <string>();

                        for (int i = 0; i < matchParams.Count; i++)
                        {
                            var val = matchParams[i].ToString();
                            if (val != "quot")
                            {
                                methodParamsMatches.Add(val);
                            }
                        }

                        if (nestedProperty.Contains("(") && !nestedProperty.Contains("."))
                        {
                            try
                            {
                                tempMethodInfo = tempNestedProperty.GetType().GetMethod(methodParamsMatches[0]);
                            }
                            catch (Exception ex)
                            {
                                ErrorHelper.LogException(ex);
                            }
                        }
                        else
                        {
                            var prop = nestedProperty;

                            var queryParamsSplit = nestedProperty.Split('?');
                            if (queryParamsSplit.Count() > 1)
                            {
                                prop = queryParamsSplit.ElementAt(0);
                            }

                            if (tempNestedProperty is JObject)
                            {
                                var val = (tempNestedProperty as JObject).GetValue(prop);

                                if (val is JArray)
                                {
                                    tempNestedProperty = String.Join(",", val as JArray);
                                }
                                else
                                {
                                    if (val != null)
                                    {
                                        tempNestedProperty = val.ToString();
                                    }
                                }
                            }
                            else
                            {
                                tempPropertyInfo = tempNestedProperty.GetType().GetProperty(prop);
                            }
                        }

                        if (tempPropertyInfo != null || tempMethodInfo != null)
                        {
                            if (tempPropertyInfo != null)
                            {
                                tempNestedProperty = tempPropertyInfo.GetValue(tempNestedProperty, null);
                            }
                            else if (tempMethodInfo != null)
                            {
                                var objParams      = new object[methodParamsMatches.Count - 1];
                                var parametersInfo = tempMethodInfo.GetParameters();

                                for (var i = 0; i < methodParamsMatches.Count - 1; i++)
                                {
                                    if (parametersInfo.Count() > i)
                                    {
                                        objParams[i] = Convert.ChangeType(methodParamsMatches[i + 1], parametersInfo[i].ParameterType);
                                    }
                                }

                                tempNestedProperty = tempMethodInfo.Invoke(tempNestedProperty, objParams.Where(i => i != null)?.ToArray());
                            }

                            if (tempNestedProperty != null)
                            {
                                var  hasEnumerable = tempNestedProperty.GetType().ToString().Contains("Enumerable") || tempNestedProperty.GetType().ToString().Contains("Collection");
                                long tmpIndex      = 0;

                                if (nestedProperties.Count() > propertyloopIndex + 1)
                                {
                                    if (hasEnumerable)
                                    {
                                        if (long.TryParse(nestedProperties[propertyloopIndex + 1], out tmpIndex))
                                        {
                                            var count = 0;
                                            foreach (var nestedItem in tempNestedProperty as IEnumerable <object> )
                                            {
                                                if (count == tmpIndex)
                                                {
                                                    tempNestedProperty = nestedItem;
                                                    break;
                                                }

                                                count++;
                                            }

                                            continue;
                                        }
                                        else
                                        {
                                            var count                = 0;
                                            var returnValue          = "";
                                            var tempPropertiesString = "";

                                            var tmp = nestedProperties.ToList();
                                            tmp.RemoveAt(propertyloopIndex);

                                            var newPropertyString = string.Join(".", tmp);

                                            foreach (var nestedItem in (dynamic)tempNestedProperty)
                                            {
                                                var tmpReturn = ParseData("{" + newPropertyString + "}", nestedItem);
                                                returnValue += ParseData(tmpReturn, nestedItem);

                                                /*var tmpReturn = ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", nestedItem);
                                                 * returnValue += ParseData(tmpReturn, nestedItem);
                                                 * count++;*/
                                            }

                                            tagValue = returnValue;
                                        }
                                    }
                                }
                                else
                                {
                                    if (nestedProperties.Length < propertyloopIndex + 1)
                                    {
                                        return(ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", tempNestedProperty));
                                    }
                                    else if (tempNestedProperty is string)
                                    {
                                        if (tempMethodInfo != null)
                                        {
                                            tagValue = tempNestedProperty.ToString();
                                        }
                                        //return ParseData("{" + nestedProperties[propertyloopIndex + 1] + "}", tempNestedProperty);
                                    }
                                }
                            }
                        }
                        else
                        {
                            var splitEq = nestedProperty.ToString().Split('=');
                            if (splitEq.Count() > 1)
                            {
                                tempPropertyInfo = tempNestedProperty.GetType().GetProperty(splitEq[0]);

                                if (tempPropertyInfo != null)
                                {
                                    var returnVal = tempPropertyInfo.GetValue(tempNestedProperty, null);

                                    if (splitEq[1].Replace("\"", "") == returnVal.ToString())
                                    {
                                        var tmp = nestedProperties.ToList();
                                        tmp.RemoveAt(propertyloopIndex);

                                        var newPropertyString = string.Join(".", tmp);

                                        tempNestedProperty = ParseData("{" + newPropertyString + "}", tempNestedProperty);
                                    }
                                    else
                                    {
                                        tempNestedProperty = "";
                                    }
                                }
                            }
                        }

                        propertyloopIndex++;
                    }

                    if (tempNestedProperty is DateTime)
                    {
                        tagValue = data.Replace(item.ToString(), StringHelper.FormatOnlyDate((DateTime)tempNestedProperty));
                    }

                    if (tempNestedProperty is string || tempNestedProperty is bool || tempNestedProperty is long)
                    {
                        var val = tempNestedProperty.ToString();

                        var queryStringSplit = item.ToString().Replace(OpenToken, "").Replace(CloseToken, "").Split('?');

                        if (queryStringSplit.Count() > 1)
                        {
                            var nv = HttpUtility.ParseQueryString(queryStringSplit.ElementAt(1));

                            foreach (string key in nv)
                            {
                                var value = nv[key];
                                val = val.Replace(OpenToken + key + CloseToken, value);
                            }
                        }

                        tagValue = data.Replace(item.ToString(), val);
                    }
                }

                if (tagValue.StartsWith("~/"))
                {
                    tagValue = URIHelper.ConvertToAbsUrl(tagValue);
                }

                if (!string.IsNullOrEmpty(tagValue))
                {
                    data = RunOrCompileRazorCode(tag, tagValue, obj, compileRazor);
                }
            }

            data = RunOrCompileRazorCode(data, data, obj, compileRazor);

            return(data);
        }