实例创建时触发事件 [英] Firing event when instance created

查看:58
本文介绍了实例创建时触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在实例创建时触发事件?在C#

How to Firing event when instance created? in C#

推荐答案

多么奇怪的要求!你不能用普通事件做到这一点,因为在实例构造函数完成之前你不能挂钩处理程序(在此之前没有挂钩处理程序的实例!)

你可以做到,但它必须是一个静态事件:

What an odd request! You can''t do it with "normal" events, because you can''t hook a handler to them until the instance constructor is finished (there is no instance to hook the handler to until then!)
You can do it, but it needs to be a Static Event:
public class XX
    {
    public static event EventHandler Created;
    public string Text { get; set; }
    public XX(string n)
        {
        Text = n;
        if (Created != null)
            {
            Created(this, null);
            }
        }
    }

然后你可以勾选它等等:

You can then hook to it and so forth:

XX.Created += new EventHandler(XX_Created);
...
XX x = new XX("ONE");
XX y = new XX("Two");

这是一个奇怪的请求。


namespace byMirHassanMoosavi / *这个ProblemSet很简单!! * /

{

#region UsingScope

使用系统;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

#endregion



#region ProblemSet

命名空间evtFiring

{

#region SolutionScope

public delegate void OnCreateEvent(); //委托声明



[Serializable()]

公共类人员

{

公共静态事件OnCreateEvent CreateEvent; //静态事件声明(委托正在使用中!)



#region Props&Field

public string Name {get;组; }

public string Family {get;组; }

公共字符串SerializableStatus = string.Empty;

#endregion



#region构造函数

public Person()// default构造函数和事件触发场所

{

this.Name =unknown-name!;

this.Family =unknown-family!;

//使用Lambda Expression声明一个匿名方法

Person.CreateEvent + = new OnCreateEvent(()= >

{

#region ChangeConsoleTheme

Console.BackgroundColor = ConsoleColor.DarkGreen;

Console.Clear( );

Console.ForegroundColor = ConsoleColor.Green;

#endregion



if(this.GetType( ).IsSerializable == true)

this.SerializableStatus =是;

else SerializableStatus =不是;



Console.WriteLine(

\\\\ n创建的人员类实例 - {0} - 使用\\\\ n参与以下信息|> \\\\\\\\\\姓名:{1} \t\r\\\
\t |>家庭:{2} \\\\\\\\\ {1} {2} HashCode Is:{3} \t\r\\\
\ t |> {1} {2}类型为:{4} \\\\\\\\\\\\\ {1} {2} {5} Serializable.\r\\\


DateTime.Now.ToString(),

this.Name,this.Family ,

this.GetHashCode()。ToString(),this.GetType()。Name,SerializableStatus);

});

/ /首先检查然后点火!!

if(CreateEvent!= null)

{

CreateEvent();

}

}

public Person(字符串名称,字符串系列)//主要构造函数

{

this .Name = name;

this.Family = family;

//使用Lambda Expression声明一个匿名方法

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 =不是;



Console.WriteLine(

\\\\ n)创建的人员类实例 - {0} - 使用\\\\ n参与以下信息|> \\\\\\\\姓名:{1} \t\r\\\
\t |>家庭:{2} \\\\\\\\\ {1} {2} HashCode Is:{3} \t\r\\\
\ t |> {1} {2}类型为:{4} \\\\\\\\\\\\\ {1} {2} {5} Serializable.\r\\\


DateTime.Now.ToString(),

this.Name,this.Family ,

this.GetHashCode()。ToString(),this.GetType()。Name,SerializableStatus);

});

/ /首先检查然后点火!!

if(CreateEvent!= null)

{

CreateEvent();

}

}

#endregion

}

#endregion



#region使用'解决方案''

类程序

{

static void Main(string [] args)

{

人p =新人(MirHassan,Moosavi);



控制台。的WriteLine(----------------------------------------------- --------------- ----------);



//人物p2 =新人物();







Console.ReadKey();



}

}

#endregion

}

#endregion

}
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
}


这篇关于实例创建时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆