Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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# 如何写入参数null异常和无效异常?_C#_Unit Testing_Testing_Exception Handling_Nunit - Fatal编程技术网

C# 如何写入参数null异常和无效异常?

C# 如何写入参数null异常和无效异常?,c#,unit-testing,testing,exception-handling,nunit,C#,Unit Testing,Testing,Exception Handling,Nunit,我正在尝试为Register CommandHandler编写一个类 public virtual void RegisterCommandHandler<T>(string messageType, Action<T, MetaData> handler) { _handlers.Add(messageType, (command, metadata) => handler((T)command, metadata)); } 请

我正在尝试为Register CommandHandler编写一个类

public virtual void RegisterCommandHandler<T>(string messageType, Action<T, MetaData> handler)
    {
        _handlers.Add(messageType, (command, metadata) => handler((T)command, metadata));

    }
请帮我写验证
谢谢大家!

您应该直接检查处理程序是否为null

if (handler == null)
    throw new ArgumentNullException("handler cannot be null", "handler");
现在出现问题的原因是,如果handler为null,则handler.ToString()将引发NullReferenceException

if (handler == null)
    throw new ArgumentNullException("handler cannot be null", "handler");