PDA

View Full Version : آموزش: Firing event when object instance created (فراخوانی رویدادی بعد از ایجاد یک Instance از کلاس



sqlserver
جمعه 06 بهمن 1391, 16:01 عصر
سلام دوستان ..در کد زیر ما بلافاصله بعد از نمونه ساختن از کلاسمون ..یک رویداد رو فراخوانی میکنیم..در کد زیر از event و delegate و Lambda Expression استفاده کردم

namespace byMirHassanMoosavi /*This ProblemSet is very Easy!!*/
{
#region UsingScope
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion

#region ProblemSet
namespace evtFiring
{
#region SolutionScope
public delegate void OnCreateEvent(); //delegate declaration

[Serializable()]
public class Person
{
public static event OnCreateEvent CreateEvent; //static event declaration (delegate in use!)

#region Props & Field
public string Name { get; set; }
public string Family { get; set; }
public string SerializableStatus = string.Empty;
#endregion

#region Constructor
public Person() //default Constructor & event firing place
{
this.Name = "unknown-name!";
this.Family = "unknown-family!";
//Using Lambda Expression for declare an anonymous method
Person.CreateEvent += new OnCreateEvent(() =>
{
#region ChangeConsoleTheme
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
#endregion

if (this.GetType().IsSerializable == true)
this.SerializableStatus = "is";
else SerializableStatus = "is not";

Console.WriteLine(
"\r\nThe Instance of Person Class Created in - {0} - with \r\nFollowing Information|>\r\n\r\n\t|> Name: {1}\t\r\n\t|> Family: {2}\t\r\n\r\n\t|> {1} {2} HashCode Is: {3}\t\r\n\t|> {1} {2} Type Is: {4}\t\r\n\t\r\n\t|> {1} {2} {5} Serializable.\r\n",
DateTime.Now.ToString(),
this.Name, this.Family,
this.GetHashCode().ToString(), this.GetType().Name, SerializableStatus);
});
//First Check then fire!!
if (CreateEvent != null)
{
CreateEvent();
}
}
public Person(string name, string family)//Main Constructor
{
this.Name = name;
this.Family = family;
//Using Lambda Expression for declare an anonymous method
Person.CreateEvent += new OnCreateEvent(() =>
{
#region ChangeConsoleTheme
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
#endregion

if (this.GetType().IsSerializable == true)
this.SerializableStatus = "is";
else SerializableStatus = "is not";

Console.WriteLine(
"\r\nThe Instance of Person Class Created in - {0} - with \r\nFollowing Information|>\r\n\r\n\t|> Name: {1}\t\r\n\t|> Family: {2}\t\r\n\r\n\t|> {1} {2} HashCode Is: {3}\t\r\n\t|> {1} {2} Type Is: {4}\t\r\n\t\r\n\t|> {1} {2} {5} Serializable.\r\n",
DateTime.Now.ToString(),
this.Name, this.Family,
this.GetHashCode().ToString(), this.GetType().Name, SerializableStatus);
});
//First Check then fire!!
if (CreateEvent != null)
{
CreateEvent();
}
}
#endregion
}
#endregion

#region Using'Solution'
class Program
{
static void Main(string[] args)
{
Person p = new Person("MirHassan", "Moosavi");

Console.WriteLine("------------------------------------------------------------------------");

//Person p2 = new Person();



Console.ReadKey();

}
}
#endregion
}
#endregion
}


کد برنامه :