jeudi 4 juin 2015

Cost of reflection: WPF

I have implemented an extension helper to load a WPF usercontrol to a window dynamically. (MyButton is in another assembly).

This helper is in a class lib that is used in all my projects. The idea is to save on re-coding this operation and keep the client code cleaner.

I would like a second pair of eyes (or more) to let me know if the cost of this to too high.

Thanks.

 public static Window OpenUserControl(this MyButton button, string controlName, string title)
        {
            //      
            object uControl;
            try
            {
                Type newType = button.GetType().Assembly.GetType(controlName,true,true);
                uControl = Activator.CreateInstance(newType);
            }
            catch (Exception e)
            {                
                throw;
            }


            // launch the usercontrol as a window.
            Window form = new Window
            {
                Title = title,
                Content = uControl,
                ShowInTaskbar = false
            };
            return form;
        }





Aucun commentaire:

Enregistrer un commentaire