using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
namespace DeletegateT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyClass<string, string> _MyClass = new MyClass<string, string>();//实例化泛型类MyClass
_MyClass._MyDelegate = new MyDelegate<string, string>(DemoMethod);//实例化_MyClass的泛型委托
MessageBox.Show(_MyClass.GetValues("Jimmy's Book","Jimmy"));//此时编译器已经能确定参数的类型为String,进而通过委托调用泛型方法DemoMethod
_MyClass._MyEvent += new MyEvent<string, string>(DemoEvent<string, string>);//给_MyClass订阅一个事件
_MyClass.InvokeEvent("Jimmy", "China");//激发事件
}
//定义有返回值的委托
public delegate string MyDelegate<T, S>(T title, S author);
//定义事件委托。
public delegate void MyEvent<E, P>(E Name, P Address);
public class MyClass<V, F>
{
//声明委托
public MyDelegate<V, F> _MyDelegate;
//声明事件委托
public event MyEvent<V, F> _MyEvent = null;
public string GetValues(V title, F author)
{
//调用委托
return _MyDelegate(title, author);
}
public MyClass()
{
}
public void InvokeEvent(V name, F address)
{
if (_MyEvent != null)
{
//调用委托
_MyEvent(name, address);
}
}
}
public string DemoMethod<T, S>(T title, S author)
{
return title.ToString() + " ,Author:" + author;
}
private void DemoEvent<V, F>(V name, F address)
{
MessageBox.Show(name + " come from " + address);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
namespace DeletegateT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyClass<string, string> _MyClass = new MyClass<string, string>();//实例化泛型类MyClass
_MyClass._MyDelegate = new MyDelegate<string, string>(DemoMethod);//实例化_MyClass的泛型委托
MessageBox.Show(_MyClass.GetValues("Jimmy's Book","Jimmy"));//此时编译器已经能确定参数的类型为String,进而通过委托调用泛型方法DemoMethod
_MyClass._MyEvent += new MyEvent<string, string>(DemoEvent<string, string>);//给_MyClass订阅一个事件
_MyClass.InvokeEvent("Jimmy", "China");//激发事件
}
//定义有返回值的委托
public delegate string MyDelegate<T, S>(T title, S author);
//定义事件委托。
public delegate void MyEvent<E, P>(E Name, P Address);
public class MyClass<V, F>
{
//声明委托
public MyDelegate<V, F> _MyDelegate;
//声明事件委托
public event MyEvent<V, F> _MyEvent = null;
public string GetValues(V title, F author)
{
//调用委托
return _MyDelegate(title, author);
}
public MyClass()
{
}
public void InvokeEvent(V name, F address)
{
if (_MyEvent != null)
{
//调用委托
_MyEvent(name, address);
}
}
}
public string DemoMethod<T, S>(T title, S author)
{
return title.ToString() + " ,Author:" + author;
}
private void DemoEvent<V, F>(V name, F address)
{
MessageBox.Show(name + " come from " + address);
}
}
}