private static K2Field CreateK2Field <TValueType>(object value)
        {
            ValueTypePart fieldPart = new ValueTypePart();

            fieldPart.Value     = value;
            fieldPart.ValueType = typeof(TValueType);

            return(CreateK2Field <TValueType>(fieldPart));
        }
        private static ValueTypePart CreateValueTypePart(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!value.GetType().IsValueType)
            {
                throw new InvalidOperationException("Value type expected.");
            }

            ValueTypePart fieldPart = new ValueTypePart();

            fieldPart.Value     = value;
            fieldPart.ValueType = value.GetType();
            return(fieldPart);
        }
Ejemplo n.º 3
0
        public static K2Field CreateConfigSettingField <T>(K2Field k2field)
        {
            K2Field newField = new K2Field();

            if (k2field != null)
            {
                newField.Parts.AddRange(k2field.Parts.ToArray());
            }

            if (newField.Parts.Count == 0)
            {
                ValueTypePart val = new ValueTypePart();
                val.Value = default(T);
                newField.Parts.Add(val);
            }

            newField.ValueType = typeof(T);
            foreach (K2FieldPart part in newField.Parts)
            {
                part.ValueType = typeof(T);
            }

            return(newField);
        }
        public static K2Field CreateK2FieldFormat(string format, params object[] args)
        {
            K2Field field = new K2Field();

            field.ValueType = typeof(string);

            if (args.Length == 0)
            {
                field.Value = format;
                return(field);
            }

            int state = 0;

            StringBuilder buffer = new StringBuilder();

            foreach (char c in format)
            {
                switch (c)
                {
                case '{':
                    if (state != 0)
                    {
                        throw new FormatException("Input string was not in a correct format.");
                    }
                    state = 1;
                    break;

                case '}':
                    if (state != 2)
                    {
                        throw new FormatException("Input string was not in a correct format.");
                    }
                    state = 3;
                    break;
                }

                switch (state)
                {
                case 0:     // normal
                    buffer.Append(c);
                    break;

                case 1:     // begin indexer
                    if (buffer.Length > 0)
                    {
                        if (field.Parts.Count > 0 && field.Parts[field.Parts.Count - 1] is ValueTypePart)
                        {
                            field.Parts[field.Parts.Count - 1].Value = string.Concat(field.Parts[field.Parts.Count - 1].Value, buffer.ToString());
                        }
                        else
                        {
                            field.Parts.Add(new ValueTypePart(buffer.ToString()));
                        }
                    }
                    buffer = new StringBuilder();
                    state  = 2;
                    break;

                case 2:     // inside indexer
                    buffer.Append(c);
                    break;

                case 3:     // end indexer
                    int idx = int.Parse(buffer.ToString());

                    if (args[idx] == null)
                    {
                        throw new InvalidOperationException("One of the arguments have a null value.");
                    }

                    K2FieldPart partToAdd = null;

                    if (args[idx].GetType().IsValueType)
                    {
                        partToAdd = new ValueTypePart(args[idx].ToString());
                    }
                    else if (args[idx] is K2FieldPart)
                    {
                        partToAdd = args[idx] as K2FieldPart;
                    }
                    else if (args[idx] is string)
                    {
                        partToAdd = new ValueTypePart(args[idx] as string);
                    }
                    else
                    {
                        throw new InvalidOperationException("One of the arguments have is an unsupported type.");
                    }

                    if (field.Parts.Count >= 1 && partToAdd is ValueTypePart)
                    {
                        // If the previous field part is also a value type part
                        // merge the values
                        if (field.Parts[field.Parts.Count - 1] is ValueTypePart)
                        {
                            field.Parts[field.Parts.Count - 1].Value = string.Concat(field.Parts[field.Parts.Count - 1].Value, partToAdd.Value);
                        }
                    }
                    else
                    {
                        field.Parts.Add(partToAdd);
                    }

                    buffer = new StringBuilder();
                    state  = 0;
                    break;
                }
            }

            // process last part of the buffer
            if (buffer.Length > 0)
            {
                if (field.Parts.Count == 0)
                {
                    field.Parts.Add(new ValueTypePart(buffer.ToString()));
                }
                else if (field.Parts[field.Parts.Count - 1] is ValueTypePart)
                {
                    field.Parts[field.Parts.Count - 1].Value = string.Concat(field.Parts[field.Parts.Count - 1].Value, buffer.ToString());
                }
                else
                {
                    field.Parts.Add(new ValueTypePart(buffer.ToString()));
                }
            }

            return(field);
        }