Esempio n. 1
0
        public static DotObjectNotationResult Parse(string toParse)
        {
            var arr = toParse.Split('=');

            if (arr.Length > 2)
            {
                throw new Exception("Invalid expression passed");
            }

            var ret = new DotObjectNotationResult();

            var pathExpression = arr[0].Split('.');

            ret.PathElements = new Queue <string>(pathExpression);

            string valueExpression = null;

            if (arr.Length == 2)
            {
                valueExpression = arr[1];
            }

            ret.Value = valueExpression;

            return(ret);
        }
Esempio n. 2
0
        public static bool Apply(DotObjectNotationResult res, object dest)
        {
            if (string.IsNullOrWhiteSpace(res.Value))
            {
                return(false);
            }

            var current = dest;

            while (res.PathElements.Count > 0)
            {
                var destPath = res.PathElements.Dequeue();

                var type = current.GetType();

                {
                    var directProp = type.GetProperty(destPath);

                    if (directProp != null)
                    {
                        if (res.PathElements.Count > 0)
                        {
                            current = directProp.GetValue(current);
                            continue;
                        }
                        return(TrySetPropertyValue(directProp, current, res.Value));
                    }
                }

                {
                    var directField = type
                                      .GetField(destPath);

                    if (directField != null)
                    {
                        if (res.PathElements.Count > 0)
                        {
                            current = directField.GetValue(current);
                            continue;
                        }

                        return(TrySetFieldValue(directField, current, res.Value));
                    }
                }

                {
                    var dataMember = type.GetProperties().Select(_ => new
                    {
                        Property   = _,
                        DataMember = _.GetCustomAttribute <DataMemberAttribute>()
                    }).FirstOrDefault(
                        _ => _.DataMember != null &&
                        !string.IsNullOrEmpty(_.DataMember.Name) &&
                        _.DataMember.Name.ToLower() == destPath.ToLower()
                        );

                    if (dataMember != null)
                    {
                        if (res.PathElements.Count > 0)
                        {
                            current = dataMember.Property.GetValue(current);
                            continue;
                        }

                        return(TrySetPropertyValue(dataMember.Property, current, res.Value));
                    }
                }

                {
                    var dataFieldMember = type.GetFields().Select(_ => new
                    {
                        Field      = _,
                        DataMember = _.GetCustomAttribute <DataMemberAttribute>()
                    }).FirstOrDefault(_ => _.DataMember != null && !string.IsNullOrEmpty(_.DataMember.Name) &&
                                      _.DataMember.Name.ToLower() == destPath.ToLower());

                    if (dataFieldMember != null)
                    {
                        if (res.PathElements.Count > 0)
                        {
                            current = dataFieldMember.Field.GetValue(current);
                            continue;
                        }

                        return(TrySetFieldValue(dataFieldMember.Field, current, res.Value));
                    }
                }
            }

            return(false);
        }