当前位置: 首页>>代码示例>>C#>>正文


C# JObject.Add方法代码示例

本文整理汇总了C#中JObject.Add方法的典型用法代码示例。如果您正苦于以下问题:C# JObject.Add方法的具体用法?C# JObject.Add怎么用?C# JObject.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在JObject的用法示例。


在下文中一共展示了JObject.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WritePropertyWithNoValue

        public void WritePropertyWithNoValue()
        {
            var o = new JObject();
            o.Add(new JProperty("novalue"));

            StringAssert.Equal(@"{
  ""novalue"": null
}", o.ToString());
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:9,代码来源:JObjectTests.cs

示例2: Remove

    public void Remove()
    {
      JObject o = new JObject();
      o.Add("PropertyNameValue", new JValue(1));
      Assert.AreEqual(1, o.Children().Count());

      Assert.AreEqual(false, o.Remove("sdf"));
      Assert.AreEqual(false, o.Remove(null));
      Assert.AreEqual(true, o.Remove("PropertyNameValue"));

      Assert.AreEqual(0, o.Children().Count());
    }
开发者ID:bitpusher,项目名称:Newtonsoft.Json4,代码行数:12,代码来源:JObjectTests.cs

示例3: GenericCollectionRemove

    public void GenericCollectionRemove()
    {
      JValue v = new JValue(1);
      JObject o = new JObject();
      o.Add("PropertyNameValue", v);
      Assert.AreEqual(1, o.Children().Count());

      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue1", new JValue(1))));
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(2))));
      Assert.AreEqual(false, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", new JValue(1))));
      Assert.AreEqual(true, ((ICollection<KeyValuePair<string, JToken>>)o).Remove(new KeyValuePair<string, JToken>("PropertyNameValue", v)));

      Assert.AreEqual(0, o.Children().Count());
    }
开发者ID:bitpusher,项目名称:Newtonsoft.Json4,代码行数:14,代码来源:JObjectTests.cs

示例4: ToJContainer

        public static JContainer ToJContainer(this NameValueCollection target)
        {
            if (target != null)
            {
                var jobj = new JObject();

                foreach (var key in target.AllKeys)
                {
                    jobj.Add(new JProperty(key, target[key]));
                }

                return jobj;
            }

            return null;
        }
开发者ID:MGramolini,项目名称:vodca,代码行数:16,代码来源:Extensions.ToJContainer.cs

示例5: TryGetValue

    public void TryGetValue()
    {
      JObject o = new JObject();
      o.Add("PropertyNameValue", new JValue(1));
      Assert.AreEqual(1, o.Children().Count());

      JToken t;
      Assert.AreEqual(false, o.TryGetValue("sdf", out t));
      Assert.AreEqual(null, t);

      Assert.AreEqual(false, o.TryGetValue(null, out t));
      Assert.AreEqual(null, t);

      Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
      Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
    }
开发者ID:bitpusher,项目名称:Newtonsoft.Json4,代码行数:16,代码来源:JObjectTests.cs

示例6: GenericCollectionCopyToInsufficientArrayCapacity

        public void GenericCollectionCopyToInsufficientArrayCapacity()
        {
            AssertException.Throws<ArgumentException>(() =>
            {
                var o = new JObject();
                o.Add("PropertyNameValue", new JValue(1));
                o.Add("PropertyNameValue2", new JValue(2));
                o.Add("PropertyNameValue3", new JValue(3));

                ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(new KeyValuePair<string, JToken>[3], 1);
            }, @"The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:12,代码来源:JObjectTests.cs

示例7: Iterate

        public void Iterate()
        {
            var o = new JObject();
            o.Add("PropertyNameValue1", new JValue(1));
            o.Add("PropertyNameValue2", new JValue(2));

            JToken t = o;

            int i = 1;
            foreach (JProperty property in t)
            {
                Assert.Equal("PropertyNameValue" + i, property.Name);
                Assert.Equal(i, (int)property.Value);

                i++;
            }
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:17,代码来源:JObjectTests.cs

示例8: DuplicatePropertyNameShouldThrow

 public void DuplicatePropertyNameShouldThrow()
 {
   JObject o = new JObject();
   o.Add("PropertyNameValue", null);
   o.Add("PropertyNameValue", null);
 }
开发者ID:bitpusher,项目名称:Newtonsoft.Json4,代码行数:6,代码来源:JObjectTests.cs

示例9: GenericCollectionCopyTo

        public void GenericCollectionCopyTo()
        {
            var o = new JObject();
            o.Add("PropertyNameValue", new JValue(1));
            o.Add("PropertyNameValue2", new JValue(2));
            o.Add("PropertyNameValue3", new JValue(3));
            Assert.Equal(3, o.Children().Count());

            KeyValuePair<string, JToken>[] a = new KeyValuePair<string, JToken>[5];

            ((ICollection<KeyValuePair<string, JToken>>)o).CopyTo(a, 1);

            Assert.Equal(default(KeyValuePair<string, JToken>), a[0]);

            Assert.Equal("PropertyNameValue", a[1].Key);
            Assert.Equal(1, (int)a[1].Value);

            Assert.Equal("PropertyNameValue2", a[2].Key);
            Assert.Equal(2, (int)a[2].Value);

            Assert.Equal("PropertyNameValue3", a[3].Key);
            Assert.Equal(3, (int)a[3].Value);

            Assert.Equal(default(KeyValuePair<string, JToken>), a[4]);
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:25,代码来源:JObjectTests.cs

示例10: IListAddBadValue

        public void IListAddBadValue()
        {
            AssertException.Throws<ArgumentException>(() =>
            {
                JProperty p1 = new JProperty("Test1", 1);
                JProperty p2 = new JProperty("Test2", "Two");
                IList l = new JObject(p1, p2);

                l.Add("Bad!");
            }, "Argument is not a JToken.");
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:11,代码来源:JObjectTests.cs

示例11: AddValueToObject

 public void AddValueToObject()
 {
   JObject o = new JObject();
   o.Add(5);
 }
开发者ID:bitpusher,项目名称:Newtonsoft.Json4,代码行数:5,代码来源:JTokenTests.cs

示例12: GenericDictionaryAdd

        public void GenericDictionaryAdd()
        {
            var o = new JObject();

            o.Add("PropertyNameValue", new JValue(1));
            Assert.Equal(1, (int)o["PropertyNameValue"]);

            o.Add("PropertyNameValue1", null);
            Assert.Equal(null, ((JValue)o["PropertyNameValue1"]).Value);

            Assert.Equal(2, o.Children().Count());
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:12,代码来源:JObjectTests.cs

示例13: IListAdd

        public void IListAdd()
        {
            JProperty p1 = new JProperty("Test1", 1);
            JProperty p2 = new JProperty("Test2", "Two");
            IList l = new JObject(p1, p2);

            JProperty p3 = new JProperty("Test3", "III");

            l.Add(p3);

            Assert.Equal(3, l.Count);
            Assert.Equal(p3, l[2]);
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:13,代码来源:JObjectTests.cs

示例14: ListChanged

        public void ListChanged()
        {
            JProperty p1 = new JProperty("Test1", 1);
            JProperty p2 = new JProperty("Test2", "Two");
            var o = new JObject(p1, p2);

            ListChangedType? changedType = null;
            int? index = null;

            o.ListChanged += (s, a) =>
            {
                changedType = a.ListChangedType;
                index = a.NewIndex;
            };

            JProperty p3 = new JProperty("Test3", "III");

            o.Add(p3);
            Assert.Equal(changedType, ListChangedType.ItemAdded);
            Assert.Equal(index, 2);
            Assert.Equal(p3, ((IList<JToken>)o)[index.Value]);

            JProperty p4 = new JProperty("Test4", "IV");

            ((IList<JToken>)o)[index.Value] = p4;
            Assert.Equal(changedType, ListChangedType.ItemChanged);
            Assert.Equal(index, 2);
            Assert.Equal(p4, ((IList<JToken>)o)[index.Value]);
            Assert.False(((IList<JToken>)o).Contains(p3));
            Assert.True(((IList<JToken>)o).Contains(p4));

            o["Test1"] = 2;
            Assert.Equal(changedType, ListChangedType.ItemChanged);
            Assert.Equal(index, 0);
            Assert.Equal(2, (int)o["Test1"]);
        }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:36,代码来源:JObjectTests.cs

示例15: DuplicatePropertyNameShouldThrow

 public void DuplicatePropertyNameShouldThrow()
 {
     AssertException.Throws<ArgumentException>(() =>
     {
         var o = new JObject();
         o.Add("PropertyNameValue", null);
         o.Add("PropertyNameValue", null);
     }, "Can not add property PropertyNameValue to OpenGamingLibrary.Json.Linq.JObject. Property with the same name already exists on object.");
 }
开发者ID:extesla,项目名称:OpenGamingLibrary,代码行数:9,代码来源:JObjectTests.cs


注:本文中的JObject.Add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。