Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过空保护的对象上的系统执行引擎异常_C#_.net Core_Nullreferenceexception_Visual Studio Debugging - Fatal编程技术网

C# 通过空保护的对象上的系统执行引擎异常

C# 通过空保护的对象上的系统执行引擎异常,c#,.net-core,nullreferenceexception,visual-studio-debugging,C#,.net Core,Nullreferenceexception,Visual Studio Debugging,您好,有人能告诉我一个对象如何通过NULLguard,然后在试图访问不可为NULL的字段时抛出异常吗?(布尔) 正如您在上面的方法中所看到的,类型为节点的对象响应不为空。请将注意力集中在布尔值字段IsArray 让我们看看当我们的响应进入反序列化方法时会发生什么: 然后在不可为空的字段上出现空引用异常。正好在我说要关注的字段上:布尔值IsArray 堆栈跟踪 {System.NullReferenceException: Object reference not set to an ins

您好,有人能告诉我一个对象如何通过
NULL
guard,然后在试图访问不可为NULL的字段时抛出异常吗?(布尔)

正如您在上面的方法中所看到的,类型为
节点的对象
响应
不为空。请将注意力集中在
布尔值
字段
IsArray

让我们看看当我们的
响应
进入
反序列化
方法时会发生什么:

然后在
不可为空的字段
上出现
空引用异常
。正好在我说要关注的字段上:
布尔值
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}
有人能解释一下这是怎么可能的吗?下面是
节点的类型:

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;
        }

    }
公共抽象部分类节点:IEquatable{
公共枚举鉴别器{
一串
整数,
排列
}
公共鉴别器种类=>this.NodeKind;
受保护的抽象鉴别器NodeKind{get;}
public Node.Array AsArray=>this as Node.Array;
public Node.Integer AsInteger=>此为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;
#伊拉克地区
受保护的虚拟只读存储器GetRaw(){
返回新字节[]{};
}
#端区
公共布尔等于(节点其他){
if(this.Kind!=其他.Kind){
返回false;
}
布尔结果;
开关(其他类型){
大小写鉴别器.Array:result=this.AsArray.Equals(other.AsArray);break;
大小写鉴别器.Integer:result=this.AsInteger.Equals(other.AsInteger);break;
大小写鉴别器.String:result=this.AsString.Equals(other.AsString);break;
默认值:抛出新的NotImplementedException();
}
返回结果;
}
}
p.S:
节点
进入
反序列化
方法时,调试器出现问题,因为我无法访问它(无法访问它的字段)

p.S 2如果我尝试在
反序列化器中添加以下行:
var values=node.GetType().GetFields().Select(x=>x.GetValue(node))
应用程序进入中断状态,我得到以下异常:

系统执行引擎异常

发布实际的、完整的异常,包括调用堆栈。您可以通过调用
Exception.ToString()
轻松地获得该结果。我敢打赌调用堆栈显示异常是由另一个线程的空保护抛出的。抛出带有解释的
ArgumentNullException
,而不是泛型
NullReferenceException()
,例如
抛出新的ArgumentNullException(nameof(node))
检查我的最后一行。如果我尝试获取字段的值,则会出现引擎异常。但是,正如您在第一个代码段中看到的,当输入方法时,字段会出现。第一个图像不会显示任何异常。请发布实际异常,包括调用堆栈。发布异常的名称根本没有帮助。并使用适当的异常,
ArgumentNullException
。您不需要访问任何字段,只需在异常句柄中记录
Exception.ToString()
的结果。在进入
反序列化
方法之前,将发布第一个图像,向您显示case中的对象不为null!它在方法内部以某种方式变为
NULL
,并引发异常。您确定没有查看其他线程吗?请张贴例外情况。从截图和不完整的信息中不可能重现问题或猜测出问题所在。如果没有调用堆栈,不可能是哪个代码路径导致了异常
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;
        }

    }