Здравствуйте, может кто-нибудь объяснить, как один объект может пройти через NULL
охранник, а затем выдать исключение при попытке доступа к ненулевому полю? (BOOL)
Как видно из приведенного выше метода, наш объект response
типа Node
не равен нулю. Обратите внимание на поле boolean
IsArray
!!

Посмотрим, что произойдет, когда наш response
войдет в метод Deserialize
:

Passes null guard !!

Затем происходит сбой с Null Reference Exception
на non-nullable field
. Именно на поле, которое я сказал, чтобы сфокусироваться: boolean
IsArray
.
Трассировка стека
{System.NullReferenceException: Object reference not set to an instance of an object.
at redius.Node.get_Kind() in D:\Work\redius\core\redius\Internals\Resp\Node.cs:line 15
at redius.Node.get_IsArray() in D:\Work\redius\core\redius\Internals\Resp\Node.cs:line 22
at redius.Deserializer.List.Deserialize(Node node) in D:\Work\redius\core\redius\Internals\Formatter\Deserializer\Deserializer.List.cs:line 19}
Может кто-нибудь объяснить, как это возможно? Ниже приведен тип Node
:
public abstract partial class Node : IEquatable<Node> {
public enum Discriminator {
String,
Integer,
Array
}
public Discriminator Kind => this.NodeKind;
protected abstract Discriminator NodeKind { get; }
public Node.Array AsArray => this as Node.Array;
public Node.Integer AsInteger => this as Node.Integer;
public Node.String AsString => this as String;
public bool IsArray => this.Kind == Discriminator.Array;
public bool IsString => this.Kind == Discriminator.Integer;
public bool IsInteger => this.Kind == Discriminator.String;
#region IRAW
protected virtual ReadOnlyMemory<byte> GetRaw() {
return new byte[] { };
}
#endregion
public bool Equals(Node other) {
if (this.Kind != other.Kind) {
return false;
}
bool result;
switch (other.Kind) {
case Discriminator.Array: result = this.AsArray.Equals(other.AsArray); break;
case Discriminator.Integer: result = this.AsInteger.Equals(other.AsInteger); break;
case Discriminator.String: result = this.AsString.Equals(other.AsString); break;
default: throw new NotImplementedException();
}
return result;
}
}
P.S: Когда Node
входит в метод Deserialize
, что-то не так с отладчиком, так как я не могу получить к нему доступ (не могу получить доступ к его полям)!
P.S 2 Если я попытаюсь добавить следующую строку в методе Deserializer
:
var values = node.GetType().GetFields().Select(x => x.GetValue(node));
приложение переходит в состояние останова, и я получаю следующее исключение:
System Execution Engine Exception