【发布时间】:2012-04-01 17:41:19
【问题描述】:
我在以下行中得到一个“空引用”异常:
MoleRuntime.SetMolePublicInstance(stub, receiverType, objReceiver, name, null);
程序正确构建和编译。没有关于该方法的任何参数的投诉。
这是 SetMolePublicInstance 的规范,来自对象浏览器:
SetMolePublicInstance(System.Delegate _stub, System.Type receiverType, object _receiver, string name, params System.Type[] parameterTypes)
以下是“Locals”的参数值:
+ stub {Method = {System.String <StaticMethodUnitTestWithDeq>b__0()}} System.Func<string>
+ receiverType {Name = "OrigValue" FullName = "OrigValueP.OrigValue"} System.Type {System.RuntimeType}
objReceiver {OrigValueP.OrigValue} object {OrigValueP.OrigValue}
name "TestString" string
parameterTypes null object[]
我知道 TestString() 不接受任何参数并返回字符串,因此作为尝试让事情正常工作的初学者,我为 SetMolePublicInstance 的最终参数指定了“null”。如前所述,这可以编译。
这是堆栈跟踪:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.ExtendedReflection.Collections.Indexable.ConvertAllToArray[TInput,TOutput](TInput[] array, Converter`2 converter)
at Microsoft.Moles.Framework.Moles.MoleRuntime.SetMole(Delegate _stub, Type receiverType, Object _receiver, String name, MoleBindingFlags flags, Type[] parameterTypes)
at Microsoft.Moles.Framework.Moles.MoleRuntime.SetMolePublicInstance(Delegate _stub, Type receiverType, Object _receiver, String name, Type[] parameterTypes)
at DeqP.Deq.Replace[T](Func`1 stub, Type receiverType, Object objReceiver, String name) in C:\0VisProjects\DecP_04\DecP\DeqC.cs:line 38
at DeqPTest.DecCTest.StaticMethodUnitTestWithDeq() in C:\0VisProjects\DecP_04\DecPTest\DeqCTest.cs:line 28
at Starter.Start.Main(String[] args) in C:\0VisProjects\DecP_04\Starter\Starter.cs:line 14
Press any key to continue . . .
为了避免空参数,我将最后的“null”更改为“parameterTypes”,如下行所示:
MoleRuntime.SetMolePublicInstance(stub, receiverType, objReceiver, name, parameterTypes);
我插入了一行:
Type [] parameterTypes = new Type[0];
使用 moles.runner.exe 会产生所需的结果 :)
【问题讨论】:
标签: moles