Exemple #1
0
        /// <summary>
        /// Gets the a regex match for the current command property interpretation type.
        /// </summary>
        /// <param name="literalCommandPart">The literal command part.</param>
        /// <param name="attribute">The attribute.</param>
        /// <param name="property">The property.</param>
        /// <returns></returns>
        private Match GetMatchForCommandInterpretType(string literalCommandPart, ACItemBindingAttribute attribute, PropertyInfo property)
        {
            switch (attribute.Type)
            {
            case CommandInterpretType.SingleArgument:
                return(new Regex("\\s(?<value>" + literalCommandPart + ")\\s",
                                 RegexOptions.IgnoreCase).Match(" " + literalCommandPart + " "));

            case CommandInterpretType.FlagSlash:
                return(new Regex("\\s/(?<value>" + attribute.LiteralName + ")\\s",
                                 RegexOptions.IgnoreCase).Match(" " + literalCommandPart + " "));

            case CommandInterpretType.Flag:
                var en = from ReflectionEnumCacheItem p in _cache.EnumCache.Enumerations where p.EnumerationType == property.PropertyType select p;
                foreach (var field in en.Single().ItemFields)
                {
                    foreach (var name in field.LiteralNames)
                    {
                        var match = new Regex("\\s(?<value>" + name + ")\\s",
                                              RegexOptions.IgnoreCase).Match(" " + literalCommandPart + " ");
                        if (match.Success)
                        {
                            return(match);
                        }
                    }
                }
                return(null);
            }

            if (literalCommandPart.ToLower().IndexOf(attribute.LiteralName) > -1)
            {
                switch (attribute.Type)
                {
                case CommandInterpretType.NameValuePairs:
                    return
                        (new Regex(attribute.LiteralName + "([\\s]{0,})([=]{1})(?<value>[a-z,A-Z,0-9,.,:]{1,})([\\s]{1})",
                                   RegexOptions.IgnoreCase).Match(literalCommandPart + " "));

                case CommandInterpretType.NameValuePairsSpace:
                    return
                        (new Regex(
                             attribute.LiteralName + "([\\s]{0,})([\\s]{1,})(?<value>[a-z,A-Z,0-9,.]{1,})([\\s]{1})",
                             RegexOptions.IgnoreCase).Match(literalCommandPart + " "));

                default:
                    throw new Exception(string.Format("Command interpretation not supported for {0}", attribute.Type));
                }
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Casts the action string command property value to a action command property.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="property">The property.</param>
        /// <param name="uncastedValue">The uncasted value.</param>
        /// <returns>false if the cast was not successfull</returns>
        private bool CastValueToProperty(Object instance, PropertyInfo property, ACItemBindingAttribute attribute, string uncastedValue)
        {
            if (attribute.Delimiter != 0)
            {
                // multivalued.
                var b = property.PropertyType.GetGenericArguments();
                switch (b[0].FullName) //todo: cast this to an actual type. instead of string.
                {
                case "System.Int32":
                    var list = new List <System.Int32>();    // make this more generic, see todo.

                    foreach (string item in uncastedValue.Split(attribute.Delimiter))
                    {
                        list.Add(int.Parse(item, ci));
                    }
                    property.SetValue(instance, list, null);
                    return(true);
                }
            }

            // bool flag.
            if (property.PropertyType == typeof(bool))
            {
                property.SetValue(instance, true, null);
                return(true);
            }


            if (property.PropertyType == typeof(int))
            {
                property.SetValue(instance, int.Parse(uncastedValue, ci), null);
                return(true);
            }
            if (property.PropertyType == typeof(float) || property.PropertyType == typeof(Single))
            {
                property.SetValue(instance, float.Parse(uncastedValue, ci), null);
                return(true);
            }
            if (property.PropertyType == typeof(string))
            {
                property.SetValue(instance, uncastedValue, null);
                return(true);
            }
            if (property.PropertyType.IsEnum)
            {
                var e = from ReflectionEnumCacheItem p in _cache.EnumCache.Enumerations
                        where p.EnumerationType == property.PropertyType
                        select p;
                if (e.Count() == 1)
                {
                    var f = e.Single().GetFieldByLiteralName(uncastedValue);
                    property.SetValue(instance, f.Value, null);
                    return(true);
                }
            }
            if (property.PropertyType == typeof(Color))
            {
                if (uncastedValue.StartsWith("#"))
                {
                    // interpret the hex raw color.
                    property.SetValue(instance, Color.FromHtml(uncastedValue), null);
                    return(true);
                }
                else
                {
                    // interpret the color by name. TODO: add color intepretation for specific aw color names.
                    property.SetValue(instance, Color.FromName(uncastedValue), null);
                    return(true);
                }
            }
            if (property.PropertyType == typeof(byte))
            {
                property.SetValue(instance, byte.Parse(uncastedValue), null);
            }
            return(false);
        }