在3.5下 public static int WordCount(this String str){}来扩展String基本类型的方法,
但在2.0下如果扩展? 

解决方案 »

  1.   

    首先说明一点,扩展方法只是语法糖,只要你使用C# 3.0就能使用,和.NET版本没有关系。在C#2.0下一样使用,只能必须得用 xxx.WordCount(str) 这样的形式。
      

  2.   

    .net2缺少 System.Core中必要的机制,这个语法糖暂时不能用。不过其它的很多都可用,例如可以用C#3.0语法在目标平台为.net2的程序中使用lamda等。
      

  3.   

    参考:http://hi.baidu.com/ysuhy/blog/item/37d6fd88ddef5aa20f244488.html
      

  4.   

    真不错。针对.net2编程的人原来也有人可怜啊。
      

  5.   


    这个要改成  str.WordCount() 这样的形式。
      

  6.   

    我不是很清楚,因为安装了.NET 3.5的电脑使用的.NET 2.0其实是屏蔽了一些库的.NET 3.5,不是真正的.NET 2.0,这两者并非完全一样。
      

  7.   

    比如我要给基本类型 String 类型扩展一个 IsTeMe()的方法
      

  8.   

    唉。随便编写一个using System;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("ad阿斯顿发发呆fa".a());
                Console.ReadKey();
            }
        }    static public class X
        {
            public static int a(this string s)
            {
                return s.Length;
            }
        }
    }namespace System.Runtime.CompilerServices
    {
        [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
        public sealed class ExtensionAttribute : Attribute { }
    }
    将目标平台设置为.net2.0平台,就可以编译通过了啊。
      

  9.   

    不管你使用vs2008还是vs2010(当然最好使用最新的),创建一个.net2.0目标平台的项目,然后就可以编译发布了。难道你为了编译一个.net2.0下的程序,还要再次安装古老的vs2005?不需要哦。而你编写的.net2.0下的程序所用的语法,不是c#2.0的,而是c#3.0的或者c#4.0的。你的c#语言的版本跟目标.net平台版本没有关系。