我在dotPeek中找到了它,位于String.cs文件中:
如果我理解正确,异常是在进入方法体之前抛出的。对吗?那么从方法中抛出NullReferenceException需要什么呢?
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[__DynamicallyInvokable]
public override bool Equals(object obj)
{
if (this == null)
throw new NullReferenceException();
string strB = obj as string;
if (strB == null)
return false;
if (object.ReferenceEquals((object) this, obj))
return true;
if (this.Length != strB.Length)
return false;
else
return string.EqualsHelper(this, strB);
}
在第二行,如果this为null,就会抛出NullReferenceException异常。那么如何才能调用空对象的方法呢?
MSDN说: 注意,应用程序会抛出ArgumentNullException异常,而不是这里讨论的NullReferenceException异常。
以下Microsoft中间语言(MSIL)指令会引发NullReferenceException异常:
callvirt
cpblk
cpobj
initblk
ldelem.<type>
ldelema
ldfld
ldflda
ldind.<type>
ldlen
stelem.<type>
stfld
stind.<type>
throw
unbox
如果我理解正确,异常是在进入方法体之前抛出的。对吗?那么从方法中抛出NullReferenceException需要什么呢?
__DynamicallyInvokableAttribute
会强制绕过任何检查来调用方法吗?还是其他原因?感谢您。