本文整理汇总了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());
}
示例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());
}
示例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());
}
示例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;
}
示例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));
}
示例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.");
}
示例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++;
}
}
示例8: DuplicatePropertyNameShouldThrow
public void DuplicatePropertyNameShouldThrow()
{
JObject o = new JObject();
o.Add("PropertyNameValue", null);
o.Add("PropertyNameValue", null);
}
示例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]);
}
示例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.");
}
示例11: AddValueToObject
public void AddValueToObject()
{
JObject o = new JObject();
o.Add(5);
}
示例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());
}
示例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]);
}
示例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"]);
}
示例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.");
}