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

推荐订阅源

小众软件
小众软件
N
News and Events Feed by Topic
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
AI
AI
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
H
Hacker News: Front Page
F
Fortinet All Blogs
博客园_首页
S
Secure Thoughts
N
News and Events Feed by Topic
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Spread Privacy
Spread Privacy
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Hugging Face - Blog
Hugging Face - Blog
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Know Your Adversary
Know Your Adversary
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Scott Helme
Scott Helme
P
Privacy & Cybersecurity Law Blog
S
Securelist
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
L
LangChain Blog
雷峰网
雷峰网
Security Archives - TechRepublic
Security Archives - TechRepublic
V2EX - 技术
V2EX - 技术

博客园 - 共同学习,共同进步

SQL Pretty Printer for SSMS 很不错的SQL格式化插件 GOOD LINK C# 代码片段 用简单代码破解Excel保护密码 罗克韦尔自动化(中国)责任有限公司 - 招聘软件开发实习生 思考1 NET Framework Library Source Code Now Available SQL IsEmptyOrNull [笔记] C# 3.0 新特性[3]-Understanding Object Initializers [笔记] C# 3.0 新特性[1]-implicitly typed local variables 通用数据库存储过程代码--高效分页存储过程 listview and downloader TFS 2008 - Running two Build Agents on the Same Machine Team Foundation Blog Overview of Team Foundation Build Understanding CGI with C# IEnumerator 是所有非泛型枚举数的基接口 C# Programming Guide An Introduction to C# Generics
[笔记] C# 3.0 新特性[2]-Understanding Extension Methods
共同学习,共同进步 · 2008-02-20 · via 博客园 - 共同学习,共同进步

namespace net30netfeature.extendMethod
{
    
using System;

    
static class MyExtensions
    
{
        
// This method allows any object to display the assembly
        
// it is defined in.
        public static void DisplayDefiningAssembly(this object obj)
        
{
            Console.WriteLine(
"{0} lives here:\n\t->{1}\n", obj.GetType().Name,
            System.Reflection.Assembly.GetAssembly(obj.GetType()));
        }

        
// This method allows any integer to reverse its digits.
        
// For example, 56 would return 65.
        public static int ReverseDigits(this int i)
        
{
            
// Translate int into a string, and then
            
// get all the characters.
            char[] digits = i.ToString().ToCharArray();
            
// Now reverse items in the array.
            Array.Reverse(digits);
            
// Put back into string.
            string newDigits = new string(digits);
            
// Finally, return the modified string back as an int.
            return int.Parse(newDigits);
        }


        
// Every Int32 now has a Foo() method
//        [System.Runtime.CompilerServices.Extension]
        internal static void Foo(this int i)
        
{ Console.WriteLine("{0} called the Foo() method.", i); }
        
// which has been overloaded to take a string!
        internal static void Foo(this int i, string msg)
        
{ Console.WriteLine("{0} called Foo() and told me: {1}", i, msg); }
    }


}


namespace net30netfeature
{
    
using System;
    
//Importing Types That Define Extension Methods
    using net30netfeature.extendMethod;
    
    
public class Extentionmethod
    
{
        
/*
         *就你所知,一个类型一但被定义编译后,就定型了。要想添加新功能方法只能通过继承,修改原代码,或通过System.Reflection.Emit
         *进行反射注入进行动态编译。
         * C# 3.0的扩展方法特性使这一需求成为了可能,扩展方法可以扩展已经存在的编译类型,使之添加新的成员,
         * 而不用更新原有的类型。
         * 这是非常有用的,当你需要注入新的功能到已经存在的类型中时,使用扩展方法,你可以添加新的功能到已经编译的类型中,
         * 来提供一个幻影,如同已经存在的类型具有这个功能方法。
         * 定义扩展方法时有三个限制:
         * 1)方法必须定义在静态类中,每个扩展方法也必须是静态方法。
         * 2)扩展方法的第一个参数必须用this关键定进行标识。
         * 3)扩展方法可以通过相应的实例方式或通过静态类方式进行调用。        
         
*/

    }
    
    
public static class TesterUtilClass
    
{
        
public static void Test()
        
{
            Test2();
        }


         
/// <summary>
        
/// Invoking Extension Methods from an Instance Level
        
/// </summary>

        public static void Test1()
        
{
            Console.WriteLine(
"***** Fun with Extension Methods *****\n");
            
// The int has assumed a new identity!
            int myInt = 12345678;
            myInt.DisplayDefiningAssembly();
            
// So has the DataSet!
            System.Data.DataSet d = new System.Data.DataSet();
            d.DisplayDefiningAssembly();
            
// And the SoundPlayer!
            System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
            sp.DisplayDefiningAssembly();
            
// Use new integer functionality.
            Console.WriteLine("Value of myInt: {0}", myInt);
            Console.WriteLine(
"Reversed digits of myInt: {0}", myInt.ReverseDigits());
            myInt.Foo();
            myInt.Foo(
"Ints that Foo? Who would have thought it!");
            
// Error! Booleans don't have the Foo() method!
            bool b2 = true;
            
// b2.Foo();
        }

        
/// <summary>
        
/// Invoking Extension Methods Statically
        
/// </summary>

        public static void Test2()
        
{
            
/*
             * 回忆一下扩展方法的第一个参数被标记为this关键字,其后跟随着方法被应用的类型,如果我们看一下
             * 这个背后发生了什么,可以用 ildasm.exe or Lutz Roeder’s Reflector来进行查看,我们会发现,编译器
             * 把实例的扩展方法的调用转换成静态方法的调用。和如下的方法的调用类似。
             
*/

            Console.WriteLine(
"***** Fun with Extension Methods *****\n");
            
int myInt = 12345678;
            MyExtensions.DisplayDefiningAssembly(myInt);
            System.Data.DataSet d 
= new System.Data.DataSet();
            MyExtensions.DisplayDefiningAssembly(d);
            System.Media.SoundPlayer sp 
= new System.Media.SoundPlayer();
            MyExtensions.DisplayDefiningAssembly(sp);
            Console.WriteLine(
"Value of myInt: {0}", myInt);
            Console.WriteLine(
"Reversed digits of myInt: {0}",
            MyExtensions.ReverseDigits(myInt));
            MyExtensions.Foo(myInt);
            MyExtensions.Foo(myInt, 
"Ints that Foo? Who would have thought it!");
        }

    }

}