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

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - 红泥

法官联系方式 记录 WORD POWER MADE EASY 数学最终讲义答案9-16章 数学最终讲义答案1-8章 数学最终讲义8-17章 数学最终讲义1-7章 数学最终讲义18 数学最终讲义16-17 数学最终讲义12-15 数学最终讲义9-11 数学最终讲义5-8 数学最终讲义1-4 数学讲义1-11章 Vocabulary 继承与多态 (翻译整理)IL教程--分支与循环 IL Basics (翻译)Introduction to Microsoft's IL 实例化和多态(胡侃)
真正的‘扩展方法’
红泥 · 2008-09-27 · via 博客园 - 红泥

标题纯属忽悠,不过是玩'this'的小把戏

C#3.0的扩展方法(静态类的静态方法):

public static class A
{
    public static int Add(this int i, int j)
    {
        return i + j;
    }
}

class tst
{
    static void Main()
    {
        Console.WriteLine(1.Add(3));
        Console.Read();
    }

}

 1.Add(3)其实就是A.Add(1,3)

俺弄得是this把戏,不过丑陋的很,仅供娱乐

一个非静态类: AgeManager,里面两个实例方法GetAge,SetAge

kkk里面的代码翻译成C#,多像:

Cat c = new Cat();

c.SetAge(11);

Console.Write(c.GetAge())

也可以:

Dog d = new Dog();

d.SetAge(6);

d.GetAge();

要再有个Pig( 只能有一个int32字段)

也能‘扩展‘一下

Cat Dog..有且只有一个int32字段

如下:

.assembly lee {}

.class private auto ansi zzz extends [mscorlib]System.Object
{
.method public  hidebysig static void kkk() il managed
{
.entrypoint
newobj instance void Cat::.ctor();
dup
ldc.i4 11
call instance void  AgeManager::SetAge(int32)
call instance int32  AgeManager::GetAge()
call void [mscorlib]System.Console::Write(int32)
ret
}
}

.class AgeManager
{
.method public hidebysig instance int32 GetAge() il managed
{
ldarga 0
ldobj int32
ldc.i4 4
add
ldobj int32
ret
}
.method public hidebysig instance void SetAge(int32) il managed
{
ldarga 0
ldobj int32
ldc.i4 4
add
ldarg.1
stobj int32
ret
}
.method public hidebysig specialname rtspecialname instance void .ctor()
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ret
}
}
.class Cat{
.field private int32 age
.method public hidebysig specialname rtspecialname instance void .ctor()
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ret
}}
.class Dog{
.field private int32 age
.method public hidebysig specialname rtspecialname instance void .ctor()
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ret
}}
其实就是把对象当this传给实例方法了,再按地址操作改了实例对应的age字段