Tuesday, April 15, 2008

Reflection , Attributue

(1). Declare customerized attrubute class ---Class

// Define the Command Handler Custom Attribute:
[AttributeUsage( AttributeTargets.Class )]
public class CommandHandlerAttribute : Attribute
{
  public CommandHandlerAttribute( )
  {
  }
}
(2).--Property

[AttributeUsage( AttributeTargets.Property ) ]
public class DynamicMenuAttribute : System.Attribute
{
  private string _menuText;
  private string _parentText;

  public DynamicMenuAttribute( string CommandText,
    string ParentText )
  {
    _menuText = CommandText;
    _parentText = ParentText;
  }

  public string MenuText
  {
    get { return _menuText; }
    set { _menuText = value; }
  }

  public string ParentText
  {
    get { return _parentText; }
    set { _parentText = value; }
  }
}

(3). Apply for the attributes:

 

[ CommandHandler ]
public class CmdHandler
{
[DynamicMenu( "Test Command", "Parent Menu" )]
public EventHandler CmdFunc
{
get
{
if ( theCmdHandler == null )
theCmdHandler = new System.EventHandler
(this.DynamicCommandHandler);
return theCmdHandler;
}
}

private void DynamicCommandHandler(
object sender, EventArgs args )
{
// Contents elided.
}
}

 


(4) Test

// Find all the assemblies in the Add-ins directory:
string AddInsDir = string.Format( "{0}/Addins", Application.StartupPath);
string[] assemblies = Directory.GetFiles( AddInsDir, "*.dll" );
foreach ( string assemblyFile in assemblies )
{
Assembly asm = Assembly.LoadFrom( assemblyFile );
// Find and install command handlers from the assembly.
foreach( System.Type t in asm.GetExportedTypes( ))
{
 if (t.GetCustomAttributes(
typeof( CommandHandlerAttribute ), false).Length > 0 )
{
// Found a command handler type:
ConstructorInfo ci =
t.GetConstructor( new Type[0] );
if ( ci == null ) // No default ctor
continue;
object obj = ci.Invoke( null );
PropertyInfo [] pi = t.GetProperties( );

// Find the properties that are command
// handlers
foreach( PropertyInfo p in pi )
{
string menuTxt = "";
string parentTxt = "";
object [] attrs = p.GetCustomAttributes(
typeof ( DynamicMenuAttribute ), false );
foreach ( Attribute at in attrs )
{
DynamicMenuAttribute dym = at as
DynamicMenuAttribute;
if ( dym != null )
{
// This is a command handler.
menuTxt = dym.MenuText;
parentTxt = dym.ParentText;
MethodInfo mi = p.GetGetMethod();
EventHandler h = mi.Invoke( obj, null )
as EventHandler;
UpdateMenu( parentTxt, menuTxt, h );
}
}
}
}
}

private void UpdateMenu( string parentTxt, string txt,
EventHandler cmdHandler )
{
MenuItem menuItemDynamic = new MenuItem();
menuItemDynamic.Index = 0;
menuItemDynamic.Text = txt;
menuItemDynamic.Click += cmdHandler;

//Find the parent menu item.
foreach ( MenuItem parent in mainMenu.MenuItems )
{
if ( parent.Text == parentTxt )
{
parent.MenuItems.Add( menuItemDynamic );
return;
}
}
// Existing parent not found:
MenuItem newDropDown = new MenuItem();
newDropDown.Text = parentTxt;
mainMenu.MenuItems.Add( newDropDown );
newDropDown.MenuItems.Add( menuItemDynamic );
}

0 Comments:

Post a Comment

<< Home