i'm not sure if your reply answer my concern. But, i just need to know if the above code fragment has some reflection overhead and why it has and how to prevent.
I am a starter for MVC tech and the following words from the book for your reference.
"If you want to avoid some reflection overhead and generate the SelectListItem collection yourself, yu can use the LINQ Select method."
EDIT - After @Bruce's post, I relooked at the source code and realized it is true. MultiSelectList has a private static method 'Eval' which internally calls
DataBinder.Eval
and this internally uses reflection to evaluate the values. Look at the note under Remark section.
"Because this method performs late-bound evaluation, using reflection at run time, it can cause performance to noticeably slow compared to standard ASP.NET data-binding syntax."
there is no version of the SelectList constructor that does not use reflection, along with rebuiling the list. if you want to avoid reflection, then do not use SelectList but rather IEnumerable<SlectListItem> instead. also as ViewBag is a dynamic wrapper
around a name/value collection, it probably slower than reflection.
model.TitleList = storeDB.Titls
.Orderby(xxxx)
.Select(g => new SelectListItem {
Text = g.Name,
Value = g.TitleId.ToString(),
Selected=album.TitleId == g.TitleId
}).ToList();
Axel_Gong
Member
4 Points
14 Posts
Any reflection overhead?
Nov 25, 2012 07:19 AM|LINK
Hello all,
Any reflection overhead on the following code? If yes, how to prevent it.
Thanks,
Axel,
ignatandrei
All-Star
134903 Points
21619 Posts
Moderator
MVP
Re: Any reflection overhead?
Nov 25, 2012 07:49 AM|LINK
Do not send select list via ViewBag - send via ViewModel
Please see http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
Axel_Gong
Member
4 Points
14 Posts
Re: Any reflection overhead?
Nov 25, 2012 09:07 AM|LINK
i'm not sure if your reply answer my concern. But, i just need to know if the above code fragment has some reflection overhead and why it has and how to prevent.
Thanks,
Axel,
stmarti
Contributor
4963 Points
1036 Posts
Re: Any reflection overhead?
Nov 25, 2012 09:34 AM|LINK
ViewBag is not reflection based, so no overhead, and nothing to prevent...
Axel_Gong
Member
4 Points
14 Posts
Re: Any reflection overhead?
Nov 25, 2012 11:37 AM|LINK
I am a starter for MVC tech and the following words from the book for your reference.
"If you want to avoid some reflection overhead and generate the SelectListItem collection yourself, yu can use the LINQ Select method."
Code:
ViewBag.Title = storeDB.Titls.Orderby(xxxx).AsEnumberable().Select(g=>new SelectListItem {Text = g.Name, Value = g.TitleId.ToString(), Selected=album.TitleId == g.TitleId});CPrakash82
All-Star
18270 Points
2839 Posts
Re: Any reflection overhead?
Nov 25, 2012 03:46 PM|LINK
I am afraid that may not be true, it does the same thing what you have here.
Look here - http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/0c48eef64d80#src%2fSystem.Web.Mvc%2fMultiSelectList.cs
EDIT - After @Bruce's post, I relooked at the source code and realized it is true. MultiSelectList has a private static method 'Eval' which internally calls DataBinder.Eval and this internally uses reflection to evaluate the values. Look at the note under Remark section.
"Because this method performs late-bound evaluation, using reflection at run time, it can cause performance to noticeably slow compared to standard ASP.NET data-binding syntax."
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: Any reflection overhead?
Nov 25, 2012 11:17 PM|LINK
there is no version of the SelectList constructor that does not use reflection, along with rebuiling the list. if you want to avoid reflection, then do not use SelectList but rather IEnumerable<SlectListItem> instead. also as ViewBag is a dynamic wrapper around a name/value collection, it probably slower than reflection.
model.TitleList = storeDB.Titls .Orderby(xxxx) .Select(g => new SelectListItem { Text = g.Name, Value = g.TitleId.ToString(), Selected=album.TitleId == g.TitleId }).ToList();