Example #1
0
        /// <summary>
        /// Get variable value by its <paramref name="name"/>
        /// </summary>
        /// <exception cref="ArgumentException">Thrown when <paramref name="name" /> is null, empty string or whitespace</exception>
        /// <exception cref="InvalidVariableException"></exception>
        public virtual object GetVariable(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException(ArgumentHelper.EmptyStringParamMessage, nameof(name));
            }

            PropertyInfo prop = _reflectionHelper.TryGetProperty(GetType(), name);

            if (prop == null)
            {
                throw new InvalidVariableException($"Cannot find variable with name \"{name}\" in class {GetType().Name} and all its parents. Variable must be public instance property.");
            }

            return(prop.GetValue(this, null));
        }
        private MemberInfo GetFirstMember(string memberName, Type type)
        {
            var          flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
            PropertyInfo prop  = _reflectionHelper.TryGetProperty(type, memberName, flags);

            if (prop != null)
            {
                return(prop);
            }

            FieldInfo field = _reflectionHelper.TryGetField(type, memberName, flags);

            if (field != null)
            {
                return(field);
            }

            throw new MemberNotFoundException($"Cannot find property or field \"{memberName}\" in class \"{type.Name}\" and all its parents. BindingFlags = {flags}");
        }