Search code examples
c#wpfsilverlightreflectionactivator

Cast ObjectHandle to UserControl


How to cast a Remoting.ObjectHandle to UserControl type ?

I would like to dynamically instanciate a UserControl :

UserControl myUserControl = (UserControl)Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, "Client.IndexView");

Error : Cannot cast expression of type 'System.Runtime.Remoting.ObjectHandle' to 'UserControl'


Solution

  • What if you use the Unwrap method:

    var instance = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, "Client.IndexView");
    UserControl myUserControl = (UserControl)instance.Unwrap();