.Net 扩展方法(此字符串 Foo)仅部分可见

发布于 2024-10-02 05:37:40 字数 1801 浏览 3 评论 0原文

这是一个关于 .Net(特别是 C#)中扩展方法可见性的问题,以及为什么智能感知可能有效,但编译器在同一代码段上失败。

想象一下...

我有 .Net 3.5 类库,由一堆对象和一个扩展方法类组成。以下是其中一种方法:

namespace MyApp.Extensions
{
    public static class ExtensionMethods
    {
        public static string ToTitleCase(this string Origcase)
        {   string TitleCase = Origcase;
            ... implementation ...
            return TitleCase;
        }
    }

要在类库本身内使用扩展方法,需要其中一种方法的每个类都需要指定:

using MyApp.Extensions;

因为这是与库的其余部分不同的命名空间。所有这些都运行良好。

现在,.Net 2.0 网站正确引用了该库,并且类文件希望使用该库。所以它声明它是:

using MyApp.Extensions

看起来工作得很好,并且当输入字符串时,Intellisense 确实看到扩展方法悬挂在字符串实例上:

http://www.flipscript.com/test/ToTitleCase.jpg

我喜欢制定计划!

然而,欢乐就这样结束了。

尝试构建网站时,构建失败并显示以下消息:

http://www. Flipscript.com/test/titlecaseerror.jpg

当尝试将 ExtensionMethods 类直接复制到 Web 项目中时,构建再次失败。这一次,因为没有预料到“这个”这个词。

奇怪的是,扩展方法确实像普通静态方法一样工作:

http://www.flipscript。 com/test/titlecaseok.jpg

...并且构建工作得很好(所以问题实际上不在于“this”关键字)。事实上,无论 ExtensionMethods 类位于类库中还是网站中,构建都可以工作。

换句话说,这确实解决了问题,并且构建将会成功。

...但解决方案很糟糕。

问题:是否有某种秘密技巧可以让扩展方法在这种情况下正常工作?

http://www.flipscript.com/test/ToTitleCase.jpg

我已经尝试了“命名空间 System.Runtime.CompilerServices”技巧,但这似乎没有帮助。

什么会导致 Intellisense 正确识别扩展方法,而编译器失败?

注意:设计的示例变量应该被称为“name”而不是“FirstName”,但您明白了。

This is a question about Extension Method visibility in .Net (specifically C#), and why intellisense may work, but the compiler fail on the same piece of code.

Picture this...

I have .Net 3.5 class library consisting of a bunch of objects and a single Extension Methods class. Here is one of the methods:

namespace MyApp.Extensions
{
    public static class ExtensionMethods
    {
        public static string ToTitleCase(this string Origcase)
        {   string TitleCase = Origcase;
            ... implementation ...
            return TitleCase;
        }
    }

To use the extension methods within the class library itself, each class that requires one of the methods needs to specify:

using MyApp.Extensions;

Since that is a different namespace than the rest of the library. All of that works fine.

Now, a .Net 2.0 web site properly references this library, and a class file wishes to use the library. So it declares that it is:

using MyApp.Extensions

That SEEMS to work just fine, and when typing a string, Intellisense does see the extension method dangling off a string instance:

http://www.flipscript.com/test/ToTitleCase.jpg

I love it when a plan comes together!

However, that's the end of the joy.

When attempting to Build the web site, the build fails with the following message:

http://www.flipscript.com/test/titlecaseerror.jpg

When attempting to directly copy the ExtensionMethods class into the web project, the build again fails. This time, because the word "this" was not expected.

Oddly enough, the extension method does work as a normal static method:

http://www.flipscript.com/test/titlecaseok.jpg

...and the Build works just fine (so the problem is NOT actually with the "this" keyword). In fact, the build works whether the ExtensionMethods class is in the class library or the web site.

In other words, this does solve the problem, and the build will succeed.

...but the solution sucks.

Question: Is there some sort of secret trick to get Extension Methods to work correctly in this scenario?

http://www.flipscript.com/test/ToTitleCase.jpg

I've tried the "namespace System.Runtime.CompilerServices" trick, but that didn't seem to help.

What would cause Intellisense to see an extension method correctly, and the compiler to fail?

Note: The contrived example variable should have been called 'name' instead of 'FirstName', but you get the idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若无相欠,怎会相见 2024-10-09 05:37:40

在 web.config 中,尝试添加到 system.web/pages/namespaces 节点

<system.web>
    ....
    <pages ....>
        ....
        <namespaces>
            ....
            <add namespace="MyApp.Extensions" />

您还需要确保 ASP.NET 编译器处于 3.5 模式:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
              type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <providerOption name="CompilerVersion" value="v3.5"/>
      <providerOption name="WarnAsError" value="false"/>
    </compiler>
 </compilers>
</system.codedom>

In web.config, try adding to the system.web/pages/namespaces node

<system.web>
    ....
    <pages ....>
        ....
        <namespaces>
            ....
            <add namespace="MyApp.Extensions" />

You will also need to ensure that the ASP.NET compiler is in 3.5 mode:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
              type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <providerOption name="CompilerVersion" value="v3.5"/>
      <providerOption name="WarnAsError" value="false"/>
    </compiler>
 </compilers>
</system.codedom>
橪书 2024-10-09 05:37:40

您的网站正在使用 .Net 2.0 运行,并且不支持扩展方法/类。所以.NET 2.0编译器将其视为普通的静态方法。它只能在 .net 3.5 中编译

Your web site is running with .Net 2.0 and it does not support extention method/class. so .NET 2.0 compiler treated it as normal static method. it will compile in .net 3.5 only

少跟Wǒ拽 2024-10-09 05:37:40

我绝不会建议将扩展方法保留在任何命名空间中。

I would never recommend keeping Extension methods inside any namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文