本文介绍了ILSpy(也称为.NET Reflector)将方法显示为只是调用自身,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将匿名类型传递给诸如TextBox之类的HtmlHelper方法时,将出现绑定错误(因为匿名类型成员仅具有内部访问权限),除非您.

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.

当我看到(HtmlHelper扩展名)InputExtensions.TextBox方法接受匿名类型时,我知道它必须在内部进行某些转换,否则将因相同的错误而失败.

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.

果然,它调用了HtmlHelper.AnonymousObjectToHtmlAttributes方法,该文档的文档试图通过不提及它来解决该问题,而建议它只是用短划线代替下划线以确保使用了有效的属性名称.当然.反正...

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...

我想确切看到转换的样子,但是当我在ILSpy中检查具有该名称的HtmlHelper静态方法时,该方法似乎只是在调用自身.这是怎么回事?

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);
}

推荐答案

System.Web.MVC.HtmlHelper中的方法AnonymousObjectToHtmlAttributes正在调用具有相同名称但来自System.Web.WebPages.Html.HtmlHelper的方法.

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

ILSpy没有明确地说明这一点.我需要将课程悬停以显示课程的来源:

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

我问了一个类似的问题,在这里

I asked a similar question that was anwered here.

这篇关于ILSpy(也称为.NET Reflector)将方法显示为只是调用自身,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!