Application["temp"] = 8;
应将值8
设置为密钥temp
。Application [“Key”]和Application.Contents [“Key”]之间的区别是什么?
然而,这样做Application.Contents["temp"] = 8;
那么什么是这两者之间的区别是什么?
非常感谢。
Application["temp"] = 8;
应将值8
设置为密钥temp
。Application [“Key”]和Application.Contents [“Key”]之间的区别是什么?
然而,这样做Application.Contents["temp"] = 8;
那么什么是这两者之间的区别是什么?
非常感谢。
目录属性是应用程序的默认,所以当你使用:
Application["temp"] = 8;
引擎盖下,上面的代码将改为调用Application.Contents["temp"] = 8
。
编辑:我刚刚使用了Reflector,并且Greg指出,Contents属性只是返回当前的HttpApplicationState
对象的引用。不确定我的答案在这种情况下是否严格正确 - 有人可以验证这一点吗?
编辑:好的,我发现当你打电话Application["temp"] = 8;
或Application.Contents["temp"] = 8;
它实际上调用HttpApplicationState.Item。看看这个IL:
.method family hidebysig instance void Page_Load(object sender, class [mscorlib]System.EventArgs e) cil managed
{
.maxstack 8
L_0000: nop
L_0001: ldarg.0
L_0002: call instance class [System.Web]System.Web.HttpApplicationState [System.Web]System.Web.UI.Page::get_Application()
L_0007: ldstr "Key"
L_000c: ldc.i4.8
L_000d: box int32
L_0012: callvirt instance void [System.Web]System.Web.HttpApplicationState::set_Item(string, object)
L_0017: nop
L_0018: ldarg.0
L_0019: call instance class [System.Web]System.Web.HttpApplicationState [System.Web]System.Web.UI.Page::get_Application()
L_001e: callvirt instance class [System.Web]System.Web.HttpApplicationState [System.Web]System.Web.HttpApplicationState::get_Contents()
L_0023: ldstr "Key"
L_0028: ldc.i4.8
L_0029: box int32
L_002e: callvirt instance void [System.Web]System.Web.HttpApplicationState::set_Item(string, object)
L_0033: nop
L_0034: ret
}
“Contents”属性的目的只是返回一个reference to the HttpApplicationState object。
它只是返回this
,所以你理论上可以做Application.Contents.Contents.Contents.Contents["temp"] = 8;
,它会做同样的事情。
只需使用Application["temp"] = 8;
。
+1使用反射器 – iTayb 2010-03-08 17:29:52