惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
F
Fortinet All Blogs
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
罗磊的独立博客
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
U
Unit 42
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
G
Google Developers Blog
H
Help Net Security

博客园 - Adrian H.

Hello in 2025 Hello World 为Silverlight控件添加鼠标滚轮支持 - Adrian H. - 博客园 LINQ表达式中关于显式范围变量的Bug jQuery太好使了 一次性下载.NET源码和pdb,并在VS2005中进行调试 Windows Server 2008: Server Core初体验 - Adrian H. C# Future Focus: 动态查找(Dynamic Lookup) Volta : Democratizing the Cloud, 1st CTP Released! Parallel Extensions for .NET 3.5 CTP! Silverlight 2.0, .NET类库源码, ASP.NET MVC... VS 2008与老版本插件的兼容性问题导致Crash Silverlight Tools for VS 2008 RTM [C# 3.0] 传递匿名类型对象的问题 Visual Studio 2008 RTM! 使用ActionScript 3中的反射,实现单元测试TestRunner ASP.NET MVC Framework Announced .NET Framework 源代码将于今年发布 N-Tiers?
在 .NET Framework x (x < 3.5) 环境下运行带有扩展方法(E...
Adrian H. · 2007-11-17 · via 博客园 - Adrian H.

扩展方法本质上只是一个编译器级别的语法糖, 但不引用.NET Framework 3.5的程序集却无法发布程序到2.0/3.0版本的运行环境中, 因为它将使那些方法(扩展方法)带上ExtensionAttribute属性, 而就是ExtensionAttribute这个类却存在于.NET Framework 3.5的程序集中.

使用一个小技巧即可以保证带有扩展方法的程序在Target到.NET Framework 2.0/3.0时通过编译, 即自己定义这个ExtensionAttribute属性以骗过编译器:

namespace System.Runtime.CompilerServices
{
    
public class ExtensionAttribute : Attribute { }
}

这样, 即使不引用3.5版本的程序集, 依然可以享受扩展方法带来的便利! 事实上, 使用VS 2008在做面向.NET Framework 2.0/3.0的开发时还可以使用许多其他C# 3.0的新语法, 如var以及强大的类型推导等:

示例代码(以下代码在VS 2008, Target to .NET Framework 2.0时编译通过):

using System;
using System.Collections.Generic;
using System.Text;// trick here~
namespace System.Runtime.CompilerServices
{
    
public class ExtensionAttribute : Attribute { }
}
namespace ConsoleApplication4
{
    
static class Extensions
    {
        
public static int ToInt(this string str)
        {
            
return int.Parse(str);
        }
public static void PrintAll<T>(this List<T> obj)
        {
            obj.ForEach(u 
=> Console.WriteLine(u)); // holy lambda!
        }
    }
class Program
    {
        
public string AutomaticProperty // automatic property!
        {
            
get;
            
set;
        }
static void Main(string[] args)
        {
            var str 
= "3"// var!
            var nine = str.ToInt() * 3// extension method!
            List<int> ints = new List<int>123 }; // collection initializer!
            ints.PrintAll(); // type inference + extension method!
            var anonymousObj = new { FieldA = "asdf", FieldB = "qwer" }; // anonymous type!
            new Program
            {
                AutomaticProperty 
= "value" // i forget this feature's name!
            };
            
// any more?
        }
    }
}

所以.. 即使你只是写面向.NET Framework 2.0的程序.. 使用VS 2008开发还是可以获得许多便利哦~

Reference: Extension Methods without 3.5 Framework