惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

博客园 - 赶路人之刚出发

集成WebSecurity的Authorize进行身份验证时,数据库连接报错问题 Html.ActionLink传递参数 Automapper结合EF实现insert,update方法 MVC中使用RemoteAttribute异步远程验证 Html.RenderPartial WebMatrix.WebSecurity创建自定义用户属性 强类型view中List<Model〉问题 params关键字 配置LINQ中的datacontext的log路径,以记录datacontext执行了的查询sql SortedList LINQ join/left join/cross join/group by/group join/sortedlist/cast Linq to objects示例 yield return 和 Func Lamda表达式 IDisposable 匿名类型与扩展方法 对象初始化器和集合初始化器 C#自动属性 .net random伪随机数
ViewBag任意属性的实现方法
赶路人之刚出发 · 2013-05-03 · via 博客园 - 赶路人之刚出发

MVC中ViewBag可以设置任意自定义的属性,也可以读取出来。

如下面这样在controller的action方法中设置了ViewBag的一个Entires属性(该属性名字任意取)后,在相应的view中也可以读取出来,那这是怎样做到的呢?

 public ActionResult Index()
        {
           
          
            var mostrecentEntiries = (from m in dbContext.test orderby m.ID descending select m).Take(10);
            ViewBag.Entires = mostrecentEntiries;         
            return View();
        }

先来看ViewBag是个什么东西?ViewBag在ControllerBase类中的定义如下:

   [Dynamic]
    public object ViewBag
    {
        [return: Dynamic]
        get
        {
            Func<ViewDataDictionary> viewDataThunk = null;
            if (this._dynamicViewDataDictionary == null)
            {
                if (viewDataThunk == null)
                {
                    viewDataThunk = delegate {
                        return this.ViewData;
                    };
                }
                this._dynamicViewDataDictionary = new DynamicViewDataDictionary(viewDataThunk);
            }
            return this._dynamicViewDataDictionary;
        }
    }

看出来了,他其实是一个DynamicViewDataDictionary类型,而DynamicViewDataDictionary得类型定义又如下:

internal sealed class DynamicViewDataDictionary : DynamicObject
{
    // Fields
    private readonly Func<ViewDataDictionary> _viewDataThunk;

    // Methods
    public DynamicViewDataDictionary(Func<ViewDataDictionary> viewDataThunk)
    {
        this._viewDataThunk = viewDataThunk;
    }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return this.ViewData.Keys;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = this.ViewData[binder.Name];
        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        this.ViewData[binder.Name] = value;
        return true;
    }

    // Properties
    private ViewDataDictionary ViewData
    {
        get
        {
            return this._viewDataThunk();
        }
    }

 

关键就是实现了上述两个绿色的方法,我们自己也来模拟一个这样的效果看,只需自定义一个继承了DynamicObject的类并重写TrySetMember,TryGetMember两个方法即可实现ViewBag同样的效果:

 class myDynamic : DynamicObject
        {
            Dictionary<string, object> ViewData;
            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                this.ViewData[binder.Name] = value;
                return true;
            }
            public override object TryGetMember(GetMemberBinder binder, object value)
            {

                return this.ViewData[binder.Name];
            }
        }
        protected void Application_Start()
        {
            dynamic myDynamic = new myDynamic();
            myDynamic.Data = "aaa";
            string stra = myDynamic.Data;