C#委托学习 原文推荐:http://www.cnblogs.com/warensoft/archive/2010/03/19/1689806.html?login=1#commentform
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
/**
* 两个RadioButton,分别用来让用户选择求最大值以及求最小值 rbtMax、rbtMin
二个TextBox,用来输入两个操作数 tbxOP1/tbxOP2
一个TextBox,用来显示运算结果 tbxResult
一个Button,用来执行运算 btGetResult
*
**/
public partial class FrmEventHandle : Form
{
delegate int MyDelegate(int a, int b);
MyDelegate md = null;
public FrmEventHandle()
{
InitializeComponent();
}
int Max(int a, int b)
{
return a > b ? a : b;
}
int Min(int a, int b)
{
return a < b ? a : b;
}
private void rbtMax_CheckedChanged(object sender, EventArgs e)
{
if (rbtMax.Checked == true)
{
this.md = new MyDelegate(this.Max);
}
}
private void rbtMin_CheckedChanged(object sender, EventArgs e)
{
if (rbtMin.Checked == true)
{
this.md = new MyDelegate(this.Min);
}
}
/// <summary>
/// btnClick事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btGetResult_Click(object sender, EventArgs e)
{
if (this.md == null)
{
MessageBox.Show("委托md没有指向任何方法!");
return;
}
int a = int.Parse(this.tbxOP1.Text);
int b = int.Parse(this.tbxOP2.Text);
//int c = this.md(a, b);
int c = this.md.Invoke(a,b);
this.tbxResult.Text = c.ToString();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmEventHandle_Load(object sender, EventArgs e)
{
this.md = new MyDelegate(this.Max);
}
///
/// 生成一个委托,用于实现多播操作
///
/// 文件的原始路径
delegate void MyMulticastDelegate(string path);
///
/// 用于生成日志文档
///
/// 文件的原始路径
static void CreateLogFile(string originalPath)
{
if (!Directory.Exists("log"))
{
Directory.CreateDirectory("log");
}
StreamWriter sw = new StreamWriter("log/log.txt", true);
sw.WriteLine("新文件已经创建,创建时间:{0},文件路径:{1}", DateTime.Now.ToLongTimeString(), originalPath);
sw.Close();
Console.WriteLine("已经写入日志!");
}
///
/// 用于将文件写入数据库
///
/// 文件的原始路径
static void WriteToDb(string originalPath)
{
FileStream fs = new FileStream(originalPath, FileMode.Open);
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=sasa");
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into tb_files values(@ID,@FileName,@CreationTime,@FileBytes)";
cmd.Parameters.Add("@ID", SqlDbType.UniqueIdentifier).Value = Guid.NewGuid();
cmd.Parameters.Add("@CreationTime", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("@FileName", SqlDbType.NText).Value = Path.GetFileName(originalPath);
cmd.Parameters.Add("@FileBytes", SqlDbType.Image).Value = buffer;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine("已经写入数据库");
}
private void button1_Click(object sender, EventArgs e)
{
//创建原始文件
StreamWriter sw = new StreamWriter("new file.txt", false);
sw.WriteLine("this is a new file");
sw.Close();
//创建委托,并指向CreateLogFile方法
MyMulticastDelegate logDelegate = new MyMulticastDelegate(CreateLogFile);
//创建委托,并指向WriteToDb方法
MyMulticastDelegate dbDelagate = new MyMulticastDelegate(WriteToDb);
MyMulticastDelegate multicastDelegate = logDelegate;
//在多播委托的调用链中添加新的委托元素
multicastDelegate = multicastDelegate + dbDelagate;
//调用多播委托,并且序列执行两个委托所指向的方法
multicastDelegate("new file.txt");
}
}
}