我的ViewData如何可以为空,但扩展在调试器? [英] How can my ViewData be null, but expandable in the debugger?

查看:350
本文介绍了我的ViewData如何可以为空,但扩展在调试器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跨越有趣的效果刚刚来到在调试视图。该方案是容易重现 - 我有一个查看断点,在监视窗口中我加 ViewBag.ViewData 和值是。不过,如果我只是添加 ViewBag 和扩大对象,我可以看到的ViewData ,这是不是。我也可以成功地展开,并查看其属性。

Just came across an interesting effect while debugging a View. The scenario is easy to reproduce - I have a breakpoint in a View, in the Watch window I add ViewBag.ViewData and the value is null. However, if I just add ViewBag and expand the object, I can see the ViewData and it is not null. I can also successfully expand it and see its properties.

谁能解释一下它是否是一个bug或者是什么原因导致这种行为?

Can anyone explain whether it's a bug or what causes this behavior?

修改

ViewBag.ViewData 其实就是。例如。如果我有这样的code。在查看:

ViewBag.ViewData is actually null. E.g. if I have this code in the View:

if (ViewBag.ViewData == null)
{
    <span>ViewBag.ViewData is null</span>
}

它显示的跨度。因此,怪异的一部分是,我可以展开在监视窗口,看看属性。

it displays the span. So the weird part is that I can expand it in the watch window and see the properties.

EDIT2

在回应@Darin季米特洛夫的答案 - 我试图复制带有自定义测试类这种行为,我收到了 RuntimeBinderException 试图访问私有财产时:'SomeClass.SomeProperty'不可访问由于其保护级别

In response to @Darin Dimitrov's answer - I tried to reproduce this behavior with a custom test class and I get a RuntimeBinderException when trying to access the private property: 'SomeClass.SomeProperty' is inaccessible due to its protection level:

public class SomeClass
{
    private string SomeProperty;
}

dynamic dynamicObject = new SomeClass();
if (dynamicObject.SomeProperty == null)
{
    Console.WriteLine("dynamicObject.SomeProperty is null");
}

在这种情况下,我不应该在预览(如果线路访问 ViewBag.ViewData (ViewBag时得到相同的异常.ViewData == NULL))?

In this case, shouldn't I be getting the same exception when accessing ViewBag.ViewData in the View (the line with if (ViewBag.ViewData == null))?

推荐答案

在调试器中看到/监视窗口是什么在私人 的ViewData ViewBag 属性。当你在视图中测试你明明有这个私人领域不能访问,你会得到null,因为没有相应的公共财产。

What you see in the debugger/watch window is the private ViewData property of the ViewBag. When you do the test in the view you obviously have no access to this private field and you get null because there is no corresponding public property.

现在在做的观点如下测试:

Now do the following test in the view:

@if (null == ViewBag
         .GetType()
         .GetProperty("ViewData", BindingFlags.Instance | BindingFlags.NonPublic)
         .GetValue(ViewBag, null)
)
{
    <span>ViewBag.ViewData is null</span>
}

和你不会看到的跨度。

当然,这一切都是很可笑的,但是当涉及到​​写现实世界和妥善架构的ASP.NET MVC应用程序都的ViewData ViewBag 无立足之地。这两个都是我的ASP.NET MVC应用程序开发的最可怕的敌人。

Of course all this is very funny but when it comes to writing real world and properly architected ASP.NET MVC applications both ViewData and ViewBag have no place. Those two are my worst enemies in ASP.NET MVC application development.

结论:始终使用视图模型和强类型的意见,并有乐趣

Conclusion: always use view models and strongly typed views and have fun.

这篇关于我的ViewData如何可以为空,但扩展在调试器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆