1.委托的标准格式
static void Main(string[] args)
{
int[] A = { 1, 2, 3, 4, -5, 6, 7, 8, 9 };
A c = new A();
c.m_Event += new M_DelegateEventHander(c_m_Event);
int iIndex=c.test(A);
Console.WriteLine(iIndex);
Console.Read();
}
static int c_m_Event(object sender, Program.m_argevent e)
{
int iIndex = -1;
for (int i = 0; i < e.A1.Length; i++)
{
if (e.A1[i] <= 0)
{
iIndex = i;
break;
}
}
return iIndex;
}
//类库的设定
public delegate int M_DelegateEventHander(object sender, m_argevent e);
//EventHandler<m_argevent>
//public delegate int M_DelegateEventHander (object sender, m_argevent e);
public class m_argevent : EventArgs
{
int[] A;
public int[] A1//获取想换换的一些数据
{
get { return A; }
set { A = value; }
}
public m_argevent(int[] M_A)
{
A1 = M_A;
}
};
public class m_class
{
public event M_DelegateEventHander m_Event;//eventhandler返回值都是空的,所以合理的利用而已
protected virtual int OnMyEvent(m_argevent e)
{
int iIndex = -1;
if (m_Event != null)
{
iIndex = m_Event(this, e);
}
return iIndex;
}
public int test(int[] A)
{
int iIndex = OnMyEvent(new m_argevent(A));
return iIndex;
}
};
public class A : m_class
{
protected override int OnMyEvent(m_argevent e)
{
int iIndex = -1;
iIndex = base.OnMyEvent(e);
return iIndex*10;
}
}
2.范型委托
class Program
{
static void Main(string[] args)
{
List<m_Point> m_list = new List<m_Point>();
//生成目标数据
for (int i = 0; i < 10; i++)
{
m_list.Add(new m_Point(i,i));
}
//进行数据的显示
for (int i = 0; i < 10; i++)
{
Console.WriteLine("({0},{1})",m_list[i].x,m_list[i].y);
}
List<int> lp = m_list.ConvertAll( new Converter<m_Point, int>(m_convert));
Console.WriteLine("下面是转化后的数据");
foreach (int idata in lp)
{
Console.WriteLine(idata);
}
Console.ReadLine();
}
static int m_convert(m_Point mlist)
{
return mlist.x + mlist.y;
}
public class m_Point
{
public int x, y;
public m_Point(int m_x,int m_y)//构造一个类型,并进行数据的初始化
{
x = m_x;
y = m_y;
}
}
文件的读写过程中的代码转换
System.Text.UTF8Encoding u = new UTF8Encoding();
byte [] byte=u.GetBytes(str);
将字符串转化为 合适的二进制代码.
解码的操作
System.Text.UTF8Encoding u = new UTF8Encoding();
String s=u.GetString(BYTE )
理解数据流的本质性质.
序列化的操作
class Program
{
static void Main(string[] args)
{
m_class m = new m_class("HYB", 10);
FileStream file = new FileStream("D://22.text", FileMode.Create, FileAccess.Write);
BinaryFormatter f = new BinaryFormatter();
f.Serialize(file, m);
file.Close();//写入文件文件
//反序列化,必须进行添加应用类型的,因为需要进行数据的转化
FileStream file2 = new FileStream("D://22.text", FileMode.Open);
BinaryFormatter f2 = new BinaryFormatter();
m_class m2 = (m_class)f.Deserialize(file2);
file2.Close();
Console.WriteLine(m2.StrName);
Console.WriteLine(m2.IAge);
Console.WriteLine(m2.b.iData);
Console.ReadLine();
}
[Serializable]
public class m_class
{
public m_classB b = new m_classB();
string strName;
public string StrName
{
get { return strName; }
set { strName = value; }
}
//[NonSerialized]
int iAge;
public int IAge
{
get { return iAge; }
set { iAge = value; }
}
public m_class(string m_strName, int m_iage)
{
strName = m_strName;
iAge = m_iage;
}
}
[Serializable]
public class m_classB
{
public int iData = 100;
}
}
//注意类的组合的序列化问题,还有就是如果某个字段不进行序列化,他的值变成默认的值
事件日志
class Program
{
delegate double DoubleOp(double x);
static void Main(string[] args)
{
if (EventLog.Exists("HYB"))
{
EventLog.CreateEventSource("mysource", "HYB");
}
EventLog e = new EventLog("HYB");
e.Source = "mysource";
e.WriteEntry("警告", EventLogEntryType.Warning);
}
//对于大型的软件来讲,除了业务系统意外,还会有公共模块,这些模块
//安全模块,数据模块,配置管理,日志模块.
}
EntryWriten是一个委托
用于日志编写过程中出发的事件
怎样开发一个windows服务程序,用windows服务来进行监控
日志的侦听程序
static void Main(string[] args)
{
EventLog e = new EventLog("HYB");
e.EntryWritten += new EntryWrittenEventHandler(e_EntryWritten);
e.EnableRaisingEvents = true;
Console.ReadLine();
}
static void e_EntryWritten(object sender, EntryWrittenEventArgs e)
{
if (e.Entry.EntryType == EventLogEntryType.Error)
{
Console.WriteLine("有异常的信息:" + e.ToString());
}
}
日志的编写程序
EventLog e = new EventLog("HYB");
e.Source = "mysource";
while (true)
{
int ag = int.Parse(Console.ReadLine());
if(ag<0)
e.WriteEntry("错误", EventLogEntryType.Error);
}
很有意思的一个东西好.