ILSpy (a.k.a. .NET Reflector) shows a method as ju

2019-08-04 19:06发布

When passing anonymous types to an HtmlHelper method like TextBox, you'll get binding errors (because anonymous type members have internal access only), unless you use a RouteDataDictionary to extract the members.

When I saw that the (HtmlHelper extension) InputExtensions.TextBox method accepted anonymous types, I knew it had to be doing some conversion internally or it would fail with the same error.

Sure enough, it calls HtmlHelper.AnonymousObjectToHtmlAttributes method, whose documentation tries to play down the issue by not mentioning it, instead suggesting it's just replacing underscores with dashes to ensure valid attribute names are used. Sure. Anyway...

I wanted to see exactly what that conversion looks like, but when I inspect HtmlHelper's static method with that name in ILSpy, the method appears to just call itself. What is going on here?

public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes)
{
    return HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
}

1条回答
看我几分像从前
2楼-- · 2019-08-04 19:30

The method AnonymousObjectToHtmlAttributes from System.Web.MVC.HtmlHelper is calling a method with the same name but from System.Web.WebPages.Html.HtmlHelper.

The ILSpy is not explicitly about that. I needed to hover the class to show from where it was coming:

enter image description here

I asked a similar question that was anwered here.

查看更多
登录 后发表回答