为什么扩展方法不适用于MVC4的Razor视图发现了什么? [英] Why is Extension Method Not Found in MVC4 Razor View?

查看:111
本文介绍了为什么扩展方法不适用于MVC4的Razor视图发现了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下字符串扩展方法

Given the following string extension method

namespace JHS.ExtensionMethods
{
  public static class StringExtensions
  {
    public static string ToUSAPhone(this String str)
    {
      return String.Format("{0:(###) ###-####}", Double.Parse(str));
    }
  }
}

一个@using声明添加到MVC4的Razor视图

A @using statement was added to the MVC4 Razor view

@using JHS.ExtensionMethods;

和下面的字符串值调用扩展方法

and the following string value calls the extension method

@Model.producer.phone.ToUSAPhone()

这导致下面的错误

which results in the following error

'string' does not contain a definition for 'ToUSAPhone'

我也试过把命名空间中的/ Views文件夹的web.config文件,并收到同样的错误。

I also tried putting the namespace in the web.config of the /Views folder and receive the same error.

<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="JHS.ExtensionMethods"/>
  </namespaces>
</pages>

我已经验证了扩展方法的工作原理是把同一个呼叫在C#类

I have verified the extension method works by putting the same call in a C# class

string test=producer.phone.ToUSAPhone();

看来参考扩展方法是不是在MVC4的Razor视图可用,但我想不出为什么?

It seems the reference to the extension method is not available in the MVC4 Razor view but I can't figure out why?

推荐答案

这会发生,如果你要使用的扩展方法的类型实际上是一个动态。检查是否正在由CSHARP RuntimeBinder产生的异常。如果是这样,您可以使用该方法作为一只普通的静态方法:

This happens if the type you are trying to use the extension method on is actually a dynamic. Check to see if the exception is being generated by the CSharp RuntimeBinder. If so, you can either use the method as a common or garden static method:

@StringExtensions.ToUSAPhone(Model.producer.phone)

或者你也可以值转换为字符串:

Or you can cast the value to a string:

@(((string)Model.producer.phone).ToUSAPhone())

<一个href=\"http://stackoverflow.com/questions/5311465/extension-method-and-dynamic-object-in-c-sharp\">According到埃里克利珀(前身MSFT):

事实上,动态不支持扩展类型背后的原因是因为在正规,非动态code扩展方法做一个完整的搜索所有已知的编译器,有一个静态类的类工作匹配的扩展方法。搜索就会按照顺序基础上,命名空间嵌套和可用的使用指令,在每个命名空间。

The reason behind the fact that dynamics do not support extension types is because in regular, non-dynamic code extension methods work by doing a full search of all the classes known to the compiler for a static class that has an extension method that match. The search goes in order based on the namespace nesting and available "using" directives in each namespace.

这意味着,为了得到正确解决了动态扩展方法调用,不知何故DLR必须知道在运行时什么都命名空间嵌套和使用指令是在源$ C ​​$ C。没有机制得心应手编码所有的信息到调用点。

That means that in order to get a dynamic extension method invocation resolved correctly, somehow the DLR has to know at runtime what all the namespace nestings and "using" directives were in your source code. There is no mechanism handy for encoding all that information into the call site.

这篇关于为什么扩展方法不适用于MVC4的Razor视图发现了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆