Closed NingLiu1998 closed 10 months ago
The Value
property is defined within the Tag<T>
class, which inherits from the Tag
class. Both of these classes can be found in the Tag.cs
file, the Tag<T>
class is found near the bottom of the file.
Practically speaking, all the defined NBT tag types inherit from Tag<T>
, and not Tag
directly, but this gives them a common base that does not require a generic, which is often cumbersome with statically-typed languages and where T
might not be known or even cared about.
I hope this helps.
The
Value
property is defined within theTag<T>
class, which inherits from theTag
class. Both of these classes can be found in theTag.cs
file, theTag<T>
class is found near the bottom of the file.Practically speaking, all the defined NBT tag types inherit from
Tag<T>
, and notTag
directly, but this gives them a common base that does not require a generic, which is often cumbersome with statically-typed languages and whereT
might not be known or even cared about.I hope this helps.
十分感谢!!! 用var用习惯了导致的问题,我知道该怎么做了
When parsing arbitrary strings into NBT, the returned object is an abstract Tag
, which will need to be inspected and cast to the more refined tag type that it actually is, as C# is a statically-typed language. There are numerous ways to do this, though the "best" way depends on your needs. Here are some common methods of doing so:
Pattern matching:
if (abstractTag is StringTag stringTag)
string value = stringTag.Value;
This method can also be used in a switch expression.
Check the Type
property, which contains a TagType
enumeration value. You could then use a switch
or if
expression to cast it directly to the matching type.
TagType enumValue = tag.Type;
switch (enumValue)
{
case (TagType.String)
StringTag stringTag = (StringTag) tag;
// ...
}
If you have your type is already known ahead of time or only one specific type of tag is acceptable for your use-case, you could just wrap it in a try/catch
and "blindly" cast it to that type, and adapt your scenario on whether the exception is thrown or not. This is going to be highly case-dependent, and really only appropriate for very specific scenarios, so I wouldn't generally recommend it.
This is a great open source! But I encountered some problems in the process of using, I don't know how to solve this problem, my technical level is relatively low
I can't find Value in "Tag", how can I get this value