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

推荐订阅源

Forbes - Security
Forbes - Security
A
Arctic Wolf
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
A
About on SuperTechFans
P
Palo Alto Networks Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
T
Tor Project blog
IT之家
IT之家
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
N
News and Events Feed by Topic
The Cloudflare Blog
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LangChain Blog
I
InfoQ
F
Full Disclosure
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
V
V2EX

博客园 - skylai

mvc3 小技巧 WCF学习之旅(一)---Hello World. WCF Client configuration Jquery表单验证 Jquery的一些常用信息 二进制、SOAP的序列化及反序列化 简单的Attribute实现. 泛型之泛型约束 C#2.0泛型介绍之简单泛型类。 序列化之XML序列化(一) Vs2005的win form动态生成菜单 强制字符换行CSS版和C#版 js操作xml 读写XML文件 js的一些杂谈 一个button同时执行多个有返回值的函数的解决方法(return false; or return true;) DataGrid里应用radio单选按钮 限制字符输入(ASCII 码) 前两天刚看书看到的,来这里跟大家分享一下有关C#处理指令的问题
C#泛型之泛型委托
skylai · 2006-11-17 · via 博客园 - skylai

在看泛型委托之前还需要先了解委托的概念。
这里讲的委托有两种类型一种是有返回值的,另一种是事件委托。

    //定义有返回值的委托 
    public delegate string GenricDelegate<T, S>(T title, S author);

    
//定义事件委托。
    public delegate void GenricDelegateEnent<E,P>(E Name,P Address);

    
public class GenericDelegateClass<V,F>
    
{
        
//声明委托
        public GenricDelegate<V, F> GdeleValue;
        
//声明事件委托
        public event GenricDelegateEnent<V, F> GdEvent = null;

        
public string GetValues(V title, F author)
        
{
            
//调用委托
            return GdeleValue(title, author);
        }


        
public GenericDelegateClass()
        
{
        }


        
public void InvokeEvent(V name, F address)
        
{
            
if (GdEvent != null)
            
{
                
//调用委托
                GdEvent(name, address);
            }

        }

    }

上面我们定义及调用了泛型委托,接下来就来梆定委托。

        private void btnDelegate_Click(object sender, EventArgs e)
        
{

            GenericDelegateClass
<stringstring> gd = new GenericDelegateClass<stringstring>();
            
//将DelegateReturn事件梆定给GdeleValue
            gd.GdeleValue = new GenricDelegate<stringstring>(DelegateReturn);
            
//将GenericEvent事件梆定给GdEvent
            gd.GdEvent += new GenricDelegateEnent<stringstring>(GenericEvent<string,string>);
        }


        
public string DelegateReturn<T,S>(T title,S author)
        
{
            
return title.ToString() + author;
        }


        
private void GenericEvent<V, F>(V name, F address)
        
{
            
//
        }

在这里我们看到我在梆定DelegateReturn的时候并没有带泛型参数。在这里的泛型参数其实是没什么意义的。因为他的类型取决于调用委托的方法的类型。也就是在前面那段代码中InvokeEvent方法的类型,这里的DelegateReturn要用泛型方法是可以随时跟InvokeEvent的参数类型保持一至。这样梆定后我们再来调用gd.GetValues("my generic post","fastyou");这样调用的其实就是DelegateReturn的方法,这就是委托的好处了,同样调用gd.InvokeEvent("my generic post","fastyou");就是GenericEvent方法。