none
Load specific userControl from DLL RRS feed

  • Question

  • Hi,

    I have created a DLL class and added two UserControls in it, UC01 and UC02.

    In my WinForm application, I want to reference to the DLL using:

    Assembly objAsm = Assembly.LoadFrom(@"C:\UC_FILES\MyDLL.dll");

    Now, how can I locate a specific user control from that DLL and add it using:

    userControl.Controls.Add(selected_uc);

    Thanks,

    Jassim

    Friday, May 29, 2015 3:27 PM

Answers

  • Using LoadFrom to load assemblies is almost universally regarded as a bad idea.  It doesn't load the assembly in the same context as the rest of the assemblies and will cause you problems. Additionally your hardcoded path won't work on all machines so you are implicitly tying your app to your machine (or any machine configured like yours).

    There are better solutions to this problem. Why exactly do you need to dynamically load this assembly rather than simply adding a reference to it in your code? If you are trying to implement some sort of DI concept then consider using a real DI container or MEF or MAF instead.  They all solve this problem without you having to write a lot of code.  More importantly though the problem that you're currently having would then go away.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Friday, June 5, 2015 3:49 PM
    Moderator

All replies

  • Hi Jassim,

    I think you are looking for something like this

    object selected_uc = objAsm.CreateInstance("YourControlFullName");

    If this answer was helpful please remember to close your threads by marking helpful posts as answer

    Fares

    Friday, May 29, 2015 4:18 PM
  • I tried the following but didn't work! and I am not getting any error!

    Assembly objAsm = Assembly.LoadFrom(@"C:\UC_FILES\MyDLL.dll");
    
    object objTemp = objAsm.CreateInstance("OPTIONS00001");
    DevExpress.XtraEditors.XtraUserControl objUserControl = objTemp as DevExpress.XtraEditors.XtraUserControl;
    
    if (objUserControl != null)
    {
        this.Controls.Add(objUserControl);
    }



    Friday, May 29, 2015 5:31 PM
  • You need to provide the full name like "namespace.OPTIONS00001"
    Friday, May 29, 2015 5:44 PM
  • Same Problem.

    object objTemp = objAsm.CreateInstance("OPTIONS00001.OPTIONS00001");

    the namespace is the same. I tried but the user control is not loaded and no error.

    this is my usercontrol:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Linq;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using DevExpress.XtraReports.UI;
    
    namespace OPTIONS00001
    {
        public partial class OPTIONS00001 : DevExpress.XtraEditors.XtraUserControl
        {
            public OPTIONS00001()
            {
                InitializeComponent();
            }
    
            private void btnPreview_Click(object sender, EventArgs e)
            {
                this.Cursor = Cursors.WaitCursor;
    
                MessageBox.Show("Yahoo!");
            }
        }
    }
    

    Friday, May 29, 2015 5:48 PM
  • There is another way of doing it by using Activator, and there is an example

    Friday, May 29, 2015 8:20 PM
  • you mean like below code? Same problem.. Nothing is shown and no error!

    Assembly assembly = Assembly.LoadFrom(@"C:\Users\CakeBoutique\Documents\Visual Studio 2012\Projects\HRMS\HRMS\bin\Release\Reports.dll");
    
    Type type = assembly.GetType("Reports.RPT00001");
    
    object instanceOfMyType = Activator.CreateInstance(type);
    
    DevExpress.XtraEditors.XtraUserControl objUserControl = instanceOfMyType as DevExpress.XtraEditors.XtraUserControl;
    
    this.Controls.Add(objUserControl);
    


    Friday, May 29, 2015 8:58 PM
  • Now we need to debug this piece of code

    1. Check out the second line if type is null or not, if null browse manually all the types in the assembly object by using GetTypes method to make sure that the assembly file is loaded correctly.
    2. Use the second overload of GetType method that take a Boolean parameter with true value to throw an exceptions if something wrong
    3. At this point if the type is not null so we need to check "instanceOfMyType" if it is null or not.


    • Edited by Fares amr Friday, May 29, 2015 10:23 PM
    Friday, May 29, 2015 10:22 PM
  • Using LoadFrom to load assemblies is almost universally regarded as a bad idea.  It doesn't load the assembly in the same context as the rest of the assemblies and will cause you problems. Additionally your hardcoded path won't work on all machines so you are implicitly tying your app to your machine (or any machine configured like yours).

    There are better solutions to this problem. Why exactly do you need to dynamically load this assembly rather than simply adding a reference to it in your code? If you are trying to implement some sort of DI concept then consider using a real DI container or MEF or MAF instead.  They all solve this problem without you having to write a lot of code.  More importantly though the problem that you're currently having would then go away.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Friday, June 5, 2015 3:49 PM
    Moderator