Esempio n. 1
0
        public Builder <T> Set(string key, object value, bool autoCast = false, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var keys     = key.Split('.');
            var property = keys[0];

            if (keys.Length > 1)
            {
                if (!_values.TryGetValue(property, out var builder))
                {
                    var(m, i) = BuilderHelper.GetMemberInfo(_value, property, bindingFlags);
                    builder   = new Builder <object>(i, m is PropertyInfo ? 1 : 2);
                    _values.Add(property, builder);
                }

                builder.Set(key.Substring(property.Length + 1), value, autoCast, bindingFlags);
                return(this);
            }


            _values.Remove(key);
            var(memberInfo, instance) = BuilderHelper.GetMemberInfo(_value, key, bindingFlags);
            memberInfo.SetValue(instance, value, autoCast);
            return(this);
        }
Esempio n. 2
0
        public static V GetValue <V>(this object self, string key, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
        {
            if (self == null)
            {
                throw new ArgumentNullException(nameof(self));
            }

            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            var(memberInfo, instance) = BuilderHelper.GetMemberInfo(self, key, bindingFlags);
            return(memberInfo.GetValue <V>(instance));
        }
Esempio n. 3
0
        public static T SetValue <T>(this T self, string key, object value, bool autoCast = false, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) where T : class
        {
            if (self == null)
            {
                throw new ArgumentNullException(nameof(self));
            }

            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            var(memberInfo, instance) = BuilderHelper.GetMemberInfo(self, key, bindingFlags);
            memberInfo.SetValue(instance, value);
            return(self);
        }