コード例 #1
0
ファイル: JsonModel.cs プロジェクト: ReedCopsey/DynamicForms
        void SetValue(JsonModel model, string key, object value)
        {
            if (commands.ContainsKey (key))
                throw new InvalidOperationException ("Cannot set the value of a command property.");

            var prop = model.Property (key);
            if (prop == null) {
                prop = new JProperty (key, value);
                model.Add(prop);
                return;
            }

            switch (prop.Value.Type) {
                case JTokenType.Integer:
                    prop.Value = Convert.ToInt32(value);
                    break;
                case JTokenType.Float:
                    prop.Value = Convert.ToSingle (value);
                    break;
                case JTokenType.String:
                    prop.Value = Convert.ToString(value);
                    break;
                case JTokenType.Boolean:
                    prop.Value = Convert.ToBoolean(value);
                    break;
                case JTokenType.Date:
                    prop.Value = Convert.ToDateTime(value);
                    break;
                case JTokenType.Guid:
                    prop.Value = (Guid)value;
                    break;
                case JTokenType.TimeSpan:
                    prop.Value = (TimeSpan)value;
                    break;
                default:
                    break;
            }
        }
コード例 #2
0
ファイル: JsonModel.cs プロジェクト: ReedCopsey/DynamicForms
        object GetValue(JsonModel model, string key)
        {
            ICommand command;
            if (commands.TryGetValue (key, out command))
                return command;

            var prop = model.Property (key);
            if (prop == null)
                return null;

            switch (prop.Value.Type) {
                case JTokenType.Integer:
                    return prop.Value.Value<int> ();
                case JTokenType.Float:
                    return prop.Value.Value<float> ();
                case JTokenType.String:
                    return prop.Value.Value<string> ();
                case JTokenType.Boolean:
                    return prop.Value.Value<bool> ();
                case JTokenType.Date:
                    return prop.Value.Value<DateTime> ();
                case JTokenType.Guid:
                    return prop.Value.Value<Guid> ();
                case JTokenType.TimeSpan:
                    return prop.Value.Value<TimeSpan> ();
                case JTokenType.Object:
                    return prop.Value.Value<JObject> ();
                case JTokenType.Array:
                    return prop.Value.Value<JArray> ();
                default:
                    return null;
            }
        }