3. Aopproxyattribute AOP Agent Features

Source: Internet
Author: User
Tags aop bool thread
In many cases, we need a singleton form, for example, Task Manager can be a singleton in the Windows application system, so we do this: add a static point member to a single piece in the implementation of the Form class. Provides a static CreateInstance method that creates a single piece when this method is called the first time. Also handle the closing event to hide the form when you click "X" in the upper-right corner of the form. You do this every time you need to implement a single form, so I decided to write a base class Isingletondisplayer, a form that doesn't have to be changed, It is convenient to simply change the original base class form to Isingletondisplayer to become a singleton form. First, the implementation in VS2003 is given.

public class Isingletondisplayer:form//Inherit from form
{
private static Isingletondisplayer singleton = null;
private static bool Toclean = FALSE;

Protected Isingletondisplayer ()
{
This. Closing + = new System.ComponentModel.CancelEventHandler (isingletondisplayer_closing);
}

#region Static for Singleton

#region Getsingleton
public static Isingletondisplayer Getsingleton ()
{
if (Isingletondisplayer.singleton!= null)
{
ISingletonDisplayer.singleton.Visible = true;
}
return Isingletondisplayer.singleton;
}

#endregion

#region Recreatesingleton

Formonui exists because a form can only be created in the main thread (UI thread).
public static void Recreatesingleton (Type targettype, object[] args, Form Formonui)
{
Type Suptype = typeof (Isingletondisplayer);
if (! Suptype.isassignablefrom (TargetType))
{
throw new Exception ("Target type isn't derived from Isingletondisplayer!");
}

   if (formonui.invokerequired)
   {
    object[] Paras = {targettype, args, Formonui};
    formonui.invoke (New Cbackcreateform (Isingletondisplayer.recreatesingleton), paras);
   }
   else
   {
    try
    {
     isingletondisplayer.toclean = false;
     ISingletonDisplayer.singleton  = (isingletondisplayer) Activator.CreateInstance (TargetType, args);
     isingletondisplayer.singleton.visible = false;
    }
    catch (Exception ee)
    {
      Throw EE;
    }
   }
  }
   #endregion

#region Destroysingleton
public static void Destroysingleton ()
{
Isingletondisplayer.toclean = true;
if (Isingletondisplayer.singleton!= null)
{
ISingletonDisplayer.singleton.Close ();
Isingletondisplayer.singleton = null;
}
}
#endregion

#endregion

#region isingletondisplayer_closing
private void Isingletondisplayer_closing (object sender, System.ComponentModel.CancelEventArgs e)
{
if (! Isingletondisplayer.toclean)
{
This. Visible = false;
E.cancel = true;
return;
}
}
#endregion
}

Internal delegate void Cbackcreateform (Type targettype, object[] args, Form Formonui);

* Use method:
* If a displayer needs to be presented in singleton mode, then you can design the displayer in the usual way, and you don't need to involve any single pieces in the design process.
* Related things. Once the design is finished, simply change the base class of the displayer from form to Isingletondisplayer. The next step is to use the displayer in a single piece mode.
*
As
* Public class appserversinfoform2:isingletondisplayer{}
* object[] args = {obj};
* Isingletondisplayer.recreatesingleton (typeof (AppServersInfoForm2), args, Formonui);

The above implementation is easy to understand, but the above implementation has a drawback because all of the single forms have to be shared by a static Isingletondisplayer Singleton member, So only one single piece of an application can inherit from Isingletondisplayer. /You can use generics to break this limit in VS2005.

The following is a look at the implementation in VS2005.

public class Isingletondisplayer<t>: Form
{
private static isingletondisplayer<t> singleton = null;
private static bool Toclean = FALSE;

Protected Isingletondisplayer ()
{
This. Closing + = new System.ComponentModel.CancelEventHandler (isingletondisplayer_closing);
}

public static isingletondisplayer<t> Getsingleton ()
{
if (Isingletondisplayer<t>.singleton!= null)
{
Isingletondisplayer<t>.singleton. Visible = true;
}
return Isingletondisplayer<t>.singleton;
}

  public static void Recreatesingleton (Type targettype, object[] args, Form Formonui)
  {
   type suptype = typeof (Isingletondisplayer);
   if (! Suptype.isassignablefrom (targettype))
   {
     throw new Exception ("Target type isn't derived from isingletondisplayer<t>!");
   }

   if (formonui.invokerequired)
   {
    object[] Paras = {targettype, args, Formonui};
    formonui.invoke New Cbackcreateform (ISINGLETONDISPLAYER<T>. Recreatesingleton), paras);
   }
   else
   {
    try
    {
     isingletondisplayer<t>.toclean = false;
     ISingletonDisplayer<T>.singleton  = (isingletondisplayer<t>) Activator.CreateInstance (TargetType, args);
     isingletondisplayer<t>.singleton. Visible = false;
    }
    catch (Exception ee)
    {
      Throw EE;
    }
   }
  }

  public static void Destroysingleton ()
  {
   isingletondisplayer <t>.toclean = true;
   if (Isingletondisplayer<t>.singleton!= null)
   {
     isingletondisplayer<t>.singleton. Close ();
    isingletondisplayer<t>.singleton = null;
   }
  } 
  
  private void Isingletondisplayer_closing (object sender, System.ComponentModel.CancelEventArgs e)
  {
   if (! Isingletondisplayer<t>.toclean)
   {
    this. Visible = false;
    e.cancel = true;
    return;
   }
  }
 
   }
 

Internal delegate void Cbackcreateform (Type targettype, object[] args, Form Formonui);

Such as:
public class appserversinfoform2:isingletondisplayer<appserversinfoform2>{}
object[] args = {obj};
Isingletondisplayer<appserversinfoform2>. Recreatesingleton (typeof (AppServersInfoForm2), args, this);

So that each single piece has its own static instance, it will not interfere with each other.

If you have a better way of doing it, welcome to discuss it with me.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.