Пример #1
0
        /// <summary>
        /// Evaluates the value as appended after the token
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <param name="state">The state.</param>
        public virtual void EvaluateDirect <T>(string value, TokenizerState <T> state)
            where T : class, new()
        {
            if (AllowPlusMinSuffix && ((value == "+") || (value == "-")))
            {
                switch (value)
                {
                case "+":
                    Evaluate("true", state);
                    break;

                case "-":
                    Evaluate("false", state);
                    break;
                }
            }
            else if (AcceptsValue && (state.TokenizerArgs.DirectValueSeparator != '\0') && value[0] == state.TokenizerArgs.DirectValueSeparator)
            {
                Evaluate(value.Substring(1), state);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Пример #2
0
        /// <summary>
        /// Gets a list with the values of this field or <c>null</c> if the value has the value specified in the <see cref="DefaultValueAttribute"/> attribute
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="state">The state.</param>
        /// <returns></returns>
        public object[] GetValues <T>(TokenizerState <T> state)
            where T : class, new()
        {
            object result = GetMemberValue(state);

            switch (TokenMemberMode)
            {
            case TokenMemberMode.Default:
                DefaultValueAttribute dva = GetFirstAttribute <DefaultValueAttribute>(_member);

                if (dva != null && dva.Value != null && dva.Value.Equals(result))
                {
                    return(null);
                }

                return(new object[] { result });

            case TokenMemberMode.Array:
            case TokenMemberMode.List:
            case TokenMemberMode.GenericList:

                List <object> items = new List <object>();
                if (result != null)
                {
                    foreach (object i in (IEnumerable)result)
                    {
                        items.Add(i);
                    }
                }
                return(items.ToArray());

            default:
                throw new InvalidOperationException();
            }
        }
Пример #3
0
        /// <summary>
        /// Evaluates the specified value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <param name="state">The state.</param>
        public virtual void Evaluate <T>(string value, TokenizerState <T> state)
            where T : class, new()
        {
            Type dataType = DataType;

            if ((value == null) && dataType != typeof(Boolean))
            {
                throw new ArgumentNullException("value", "Value must be set for non-booleans");
            }
            else if (value == null)
            {
                value = bool.TrueString;
            }

            object RawValue = ConvertValue(value, state);


            ITokenizerExpandCollection expandable = RawValue as ITokenizerExpandCollection;

            if (expandable != null)
            {
                foreach (object o in expandable)
                {
                    Member.SetValue(state, o);
                }
            }

            Member.SetValue(state, RawValue);
        }
Пример #4
0
        /// <summary>
        /// Converts value to the <see cref="DataType"/> Type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <param name="state">The state.</param>
        /// <returns></returns>
        public virtual object ConvertValue <T>(string value, TokenizerState <T> state)
            where T : class, new()
        {
            if (typeof(string) == DataType)
            {
                return(value);
            }
            else if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            TypeConverter tc = TypeConverter;

            if (tc == null)
            {
                tc = TypeDescriptor.GetConverter(DataType);
            }

            object v = tc.ConvertFromString(value);

            if ((v != null) && DataType.IsAssignableFrom(v.GetType()))
            {
                return(v);
            }
            else if (v is ITokenizerExpandCollection)             // FileSystemInfo converted star object
            {
                return(v);
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The typeconverter of type {0} (A {1} instance) can't convert a string into a {0}", DataType.FullName, tc.GetType().FullName, DataType.Name));
        }
Пример #5
0
        /// <summary>
        /// Token allows a direct value ('-v1' vs  '-v' | '-v 12' | '-v=12')
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <param name="state">The state.</param>
        /// <returns></returns>
        public virtual bool AllowDirectValue <T>(string value, TokenizerState <T> state)
            where T : class, new()
        {
            if (AllowPlusMinSuffix && ((value == "+") || (value == "-")))
            {
                return(true);
            }
            else if (AcceptsValue && (state.TokenizerArgs.DirectValueSeparator != '\0') && value[0] == state.TokenizerArgs.DirectValueSeparator)
            {
                return(true);
            }

            return(false);
        }
Пример #6
0
        internal string GetStringValue <T>(object value, TokenizerState <T> state)
            where T : class, new()
        {
            string val = value as string;

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

            TypeConverter tc = TypeConverter;

            if (tc == null)
            {
                tc = TypeDescriptor.GetConverter(DataType);
            }

            return(tc.ConvertToString(state, state.CultureInfo, value));
        }
Пример #7
0
 void SetMemberValue <T>(TokenizerState <T> state, object value)
     where T : class, new()
 {
     typeof(T).InvokeMember(Name, _isProperty ? BindingFlags.SetProperty : BindingFlags.SetField, null, state.Instance, new object[] { value }, CultureInfo.InvariantCulture);
 }
Пример #8
0
 object GetMemberValue <T>(TokenizerState <T> state)
     where T : class, new()
 {
     return(typeof(T).InvokeMember(Name, _isProperty ? BindingFlags.GetProperty : BindingFlags.GetField, null, state.Instance, null, CultureInfo.InvariantCulture));
 }
Пример #9
0
        /// <summary>
        /// Sets the value of the token.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="state">The state.</param>
        /// <param name="value">The value.</param>
        public void SetValue <T>(TokenizerState <T> state, object value)
            where T : class, new()
        {
            if (value != null && !DataType.IsAssignableFrom(value.GetType()))
            {
                throw new ArgumentException("Invalid value", "value");
            }

            switch (TokenMemberMode)
            {
            case TokenMemberMode.Default:
                SetMemberValue(state, value);
                break;

            case TokenMemberMode.Array:
            {
                Array a = (Array)GetMemberValue(state);
                Array aNew;

                if (a != null)
                {
                    aNew = Array.CreateInstance(FieldType.GetElementType(), a.Length + 1);
                    if (a.Length >= 1)
                    {
                        a.CopyTo(aNew, 0);
                    }
                }
                else
                {
                    aNew = Array.CreateInstance(FieldType.GetElementType(), 1);
                }

                aNew.SetValue(value, aNew.Length - 1);

                SetMemberValue(state, aNew);
                break;
            }

            case TokenMemberMode.List:
            case TokenMemberMode.GenericList:
            {
                object l = GetMemberValue(state);

                if (l == null)
                {
                    l = Activator.CreateInstance(FieldType);

                    SetMemberValue(state, l);
                }

                if (TokenMemberMode == TokenMemberMode.List)
                {
                    IList list = (IList)l;

                    list.Add(value);
                }
                else
                {
                    _listType.InvokeMember("Add", BindingFlags.InvokeMethod, null, l, new object[] { value }, CultureInfo.InvariantCulture);
                }
                break;
            }

            default:
                throw new InvalidOperationException();
            }
        }