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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 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) 环境下运行带有扩展方法(Extension Method)的程序
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