字符串,对象>匿名类和的IDictionary℃之间的差异;在ASP.NET MVC htmlAttributes?
例如,如果您选中这两个扩展方法,唯一的区别是htmlAttributes的类型,因此你可以通过你的htmlAttributes两种不同的方式:
For example if you check these two extension methods the only difference is type of htmlAttributes so you can pass your htmlAttributes in two different ways:
public static MvcHtmlString TextBoxFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IDictionary<string, object> htmlAttributes);
public static MvcHtmlString TextBoxFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
object htmlAttributes);
和通过下列方式使用它们:
And use them in either of these ways:
@Html.TextBoxFor(model => model.TagLine,
new { @placeholder = "We live to make art." })
@Html.TextBoxFor(model => model.TagLine,
new Dictionary<string, object> {
{ "placeholder", "We live to make art." } })
我已经检查MVC源$ C $ c和我知道他们使用同样的方法为背景,但它接受匿名对象使用一个 HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
使匿名对象字典。
在我的角度来看,视图是清洁剂使用匿名对象。你怎么看你们?是否有任何缺点,使用一个匿名对象?
In my point of view, views are cleaner to use anonymous object. What do you think guys? Are there any drawbacks to using an anonymous object?
有没有太多的区别,但用匿名对象是调用者更清洁,更可读的语法,现在算是比较标准的做法。有可能是轻微的,可以忽略不计的性能优势,如果你是微优化的风扇使用的IDictionary。
There's not too much difference, however using the anonymous object is a cleaner and more readable syntax for the caller, and now considered more standard practice. There might be a slight, negligible performance benefit to using IDictionary if you're a fan of micro-optimisation.
在IDictionary的超载选项卡可能因为ASP.NET MVC 1.0 CTP天,当C#3.0和匿名对象仍然很新。 Eilon立顿的博客文章建议Using C#3.0匿名类型的字典给出了一些背景。
The IDictionary overload option has probably stuck since ASP.NET MVC 1.0 CTP days when C# 3.0 and anonymous objects were still quite new. Eilon Lipton's blog post proposing Using C# 3.0 Anonymous Types as Dictionaries gives some background.