首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

.net freamwork 2.0 下如果扩展基本类型解决方案

2012-04-12 
.net freamwork 2.0 下如果扩展基本类型在3.5下 public static int WordCount(this String str){}来扩展St

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

[解决办法]
参考:http://hi.baidu.com/ysuhy/blog/item/37d6fd88ddef5aa20f244488.html
[解决办法]

探讨
在C#2.0下一样使用,只能必须得用 xxx.WordCount(str) 这样的形式。

[解决办法]
唉。随便编写一个
C# code
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 { }}
[解决办法]
不管你使用vs2008还是vs2010(当然最好使用最新的),创建一个.net2.0目标平台的项目,然后就可以编译发布了。难道你为了编译一个.net2.0下的程序,还要再次安装古老的vs2005?不需要哦。

而你编写的.net2.0下的程序所用的语法,不是c#2.0的,而是c#3.0的或者c#4.0的。你的c#语言的版本跟目标.net平台版本没有关系。

热点排行