本文整理汇总了C#中NbtCompound.TryGet方法的典型用法代码示例。如果您正苦于以下问题:C# NbtCompound.TryGet方法的具体用法?C# NbtCompound.TryGet怎么用?C# NbtCompound.TryGet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NbtCompound
的用法示例。
在下文中一共展示了NbtCompound.TryGet方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTextValue
private string GetTextValue(NbtCompound compound, string key)
{
NbtString text;
compound.TryGet(key, out text);
return text != null ? (text.StringValue ?? string.Empty) : string.Empty;
}
示例2: GettersAndSetters
public void GettersAndSetters()
{
// construct a document for us to test.
var nestedChild = new NbtCompound("NestedChild");
var nestedInt = new NbtInt(1);
var nestedChildList = new NbtList("NestedChildList") {
nestedInt
};
var child = new NbtCompound("Child") {
nestedChild,
nestedChildList
};
var childList = new NbtList("ChildList") {
new NbtInt(1)
};
var parent = new NbtCompound("Parent") {
child,
childList
};
// Accessing nested compound tags using indexers
Assert.AreEqual(parent["Child"]["NestedChild"], nestedChild);
Assert.AreEqual(parent["Child"]["NestedChildList"], nestedChildList);
Assert.AreEqual(parent["Child"]["NestedChildList"][0], nestedInt);
// Accessing nested compound tags using Get and Get<T>
Assert.AreEqual(parent.Get<NbtCompound>("Child").Get<NbtCompound>("NestedChild"), nestedChild);
Assert.AreEqual(parent.Get<NbtCompound>("Child").Get<NbtList>("NestedChildList"), nestedChildList);
Assert.AreEqual(parent.Get<NbtCompound>("Child").Get<NbtList>("NestedChildList")[0], nestedInt);
Assert.AreEqual((parent.Get("Child") as NbtCompound).Get("NestedChild"), nestedChild);
Assert.AreEqual((parent.Get("Child") as NbtCompound).Get("NestedChildList"), nestedChildList);
Assert.AreEqual((parent.Get("Child") as NbtCompound).Get("NestedChildList")[0], nestedInt);
// Accessing with Get<T> and an invalid given type
Assert.Throws<InvalidCastException>(() => parent.Get<NbtInt>("Child"));
// Using TryGet and TryGet<T>
NbtTag dummyTag;
Assert.IsTrue(parent.TryGet("Child", out dummyTag));
NbtCompound dummyCompoundTag;
Assert.IsTrue(parent.TryGet("Child", out dummyCompoundTag));
// Trying to use integer indexers on non-NbtList tags
Assert.Throws<InvalidOperationException>(() => parent[0] = nestedInt);
Assert.Throws<InvalidOperationException>(() => nestedInt[0] = nestedInt);
// Trying to use string indexers on non-NbtCompound tags
Assert.Throws<InvalidOperationException>(() => childList["test"] = nestedInt);
Assert.Throws<InvalidOperationException>(() => nestedInt["test"] = nestedInt);
// Trying to get a non-existent element by name
Assert.IsNull(parent.Get<NbtTag>("NonExistentTag"));
Assert.IsNull(parent["NonExistentTag"]);
// Null indices on NbtCompound
Assert.Throws<ArgumentNullException>(() => parent.Get<NbtTag>(null));
Assert.Throws<ArgumentNullException>(() => parent[null] = new NbtInt(1));
Assert.Throws<ArgumentNullException>(() => nestedInt = (NbtInt)parent[null]);
// Out-of-range indices on NbtList
Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = (NbtInt)childList[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => childList[-1] = new NbtInt(1));
Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = childList.Get<NbtInt>(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = (NbtInt)childList[childList.Count]);
Assert.Throws<ArgumentOutOfRangeException>(() => nestedInt = childList.Get<NbtInt>(childList.Count));
// Using setter correctly
parent["NewChild"] = new NbtByte("NewChild");
// Using setter incorrectly
object dummyObject;
Assert.Throws<ArgumentNullException>(() => parent["Child"] = null);
Assert.NotNull(parent["Child"]);
Assert.Throws<ArgumentException>(() => parent["Child"] = new NbtByte("NotChild"));
Assert.Throws<InvalidOperationException>(() => dummyObject = parent[0]);
Assert.Throws<InvalidOperationException>(() => parent[0] = new NbtByte("NewerChild"));
// Try adding tag to self
var selfTest = new NbtCompound("SelfTest");
Assert.Throws<ArgumentException>(() => selfTest["SelfTest"] = selfTest);
// Try adding a tag that already has a parent
Assert.Throws<ArgumentException>(() => selfTest[child.Name] = child);
}