Extension Method per semplificare la Reflection

per esigenze personali, che poi condividerò tramite script o blog, avevo la necessità di assegnare runtime un event handler per una serie di controlli.

per rendere il codice più semplice e riutilizzabile ho deciso di realizzare 4 semplici Extension Method che sono più o meno questi:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using ReflectionExtension;

namespace ReflectionExtension
{
    public static class ObjectReflectionExtension
    {
        public static void AddHanlder(Object objectEventRaiser, object objectBoundToDelegate, string methodName, string eventName)
        {
            if (objectEventRaiser == null | objectBoundToDelegate == null | string.IsNullOrEmpty(methodName) | string.IsNullOrEmpty(eventName) )
            {
                throw new ArgumentNullException();
            }
            else
            {
                Type _dataSourceType = objectEventRaiser.GetType();

                EventInfo _eventInfo = _dataSourceType.GetEvent(eventName);
               
                if (_eventInfo == null)
                {
                    throw new ArgumentException(string.Format("Impossibile trovare l'evento {0}", eventName));
                }
                else
                {
                    MethodInfo _methodInfo = objectBoundToDelegate.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);

                    if (_eventInfo == null)
                    {
                        throw new ArgumentException(string.Format("Impossibile trovare il metodo {0}", methodName));
                    }
                    else
                    {
                        Delegate _delegate = Delegate.CreateDelegate(_eventInfo.EventHandlerType, objectBoundToDelegate, _methodInfo);

                        _eventInfo.AddEventHandler(objectEventRaiser, _delegate);
                    }
                }
            }
        }
    }
}

namespace WebControlReflectionExtension
{
    public static class WebControlReflectionExtension
    {

        public static void AddInsertedHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
        {
            ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Inserted");
        }

        public static void AddDeleteddHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
        {
            ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Deleted");
        }

        public static void AddUpdateddHandler(this DataSourceControl dataSourceControl, object objectBoundToDelegate, string methodName)
        {
            ObjectReflectionExtension.AddHanlder(dataSourceControl, objectBoundToDelegate, methodName, "Updated");
        }
    }
}


associare ad un oggetto che erediti da DataSourceControl un evento tramite reflection diventa davverso semplice:

dataSourceControl.AddInsertedHandler(this, "InsertedHandler");

nati a supporto di Linq gli Extension Method risulatno davvero utili a scriver codice manutenibile e altamente riutilizzabile.
Nella stessa categoria

Commenti

Aggiungi un nuovo commento »»»
Per inserire un commento, devi registrarti alla nostra community.

© 1998-2008 - nostromo - Il blog di Marco Leoncini

TagCloud
BLOG INFO
  • Post: 222
  • Commenti: 88
  • TrackBacks: 17
  • Feed blog e contenuti tecnici: RSS
  • Feed blog: RSS Atom OPML

MVP
CATEGORIE
I PIÙ LETTI DEL MESE
IN EVIDENZA