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

推荐订阅源

Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
S
Securelist
The Hacker News
The Hacker News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
Jina AI
Jina AI
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
Webroot Blog
Webroot Blog
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
量子位
S
Schneier on Security
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
O
OpenAI News
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
博客园 - 叶小钗
L
LINUX DO - 最新话题
V
Visual Studio Blog
U
Unit 42
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Security Affairs
AWS News Blog
AWS News Blog
S
Secure Thoughts
腾讯CDC
Cloudbric
Cloudbric
H
Help Net Security
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
C
Cyber Attacks, Cyber Crime and Cyber Security
WordPress大学
WordPress大学
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 【当耐特】
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
D
DataBreaches.Net
A
About on SuperTechFans
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Martin Fowler
Martin Fowler
Vercel News
Vercel News
Cisco Talos Blog
Cisco Talos Blog
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary

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

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!");
        }

    }

}