.Net扩展方法(此字符串Foo)仅部分可见 [英] .Net Extension Method (this string Foo) Only Partially Visible

查看:86
本文介绍了.Net扩展方法(此字符串Foo)仅部分可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

为这个......

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

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.

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

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

SEEMS可以正常工作,并且在键入字符串时,Intellisense确实看到扩展方法悬挂在字符串实例上:

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

当尝试直接将ExtensionMethods类复制到Web项目时,构建再次失败.这次是因为没有想到"this"这个词.

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

...并且Build正常工作(因此,问题实际上与"this"关键字无关).实际上,无论ExtensionMethods类是在类库中还是在网站中,该构建都有效.

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

我尝试了命名空间System.Runtime.CompilerServices"技巧,但这似乎无济于事.

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

什么会导致Intellisense正确看到扩展方法,并且编译器失败?

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.config中,尝试添加到system.web/pages/namespaces节点

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

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

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

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>

这篇关于.Net扩展方法(此字符串Foo)仅部分可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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