MGZ NetArt

Software Development and Support

Home
Design Patterns
C# Tips
AsyncCallback
Asynchronous Form.ShowDia
BackgroundWorker
Connection String
CopyTo() vs Clone()
Convert Color To String
Dispose vs Finalize
Debug mode
Delete Duplicate in SQL
Drag&Drop for TreeView
Edit Full Path Property
Hiding And Overriding
Polimorphism
How to use Resources
Run Time DLL
Simple Threading
Singleton Connection
Save Win Form as JPG File
Save Object in Registry
Soap Serialization
Sort and CompareTo
Working With List<>
Examples & Videos
MRDS Examples
Resume
Contact Us

Load Dll during a Run Time

1. Create RemoteLoaderCS.cs

 

using System;

using System.Reflection;

using System.Collections.Generic;

 

using GeneralObjects;

 

namespace RemoteLoaderCS

{

    /// <summary>

    /// Interface that can be run over the remote AppDomain boundary.

    /// </summary>

    public interface IRemoteInterface

    {

        object Invoke(string lcMethod, params object[] Parameters);

    }

 

 

    /// <summary>

    /// Factory class to create objects exposing IRemoteInterface

    /// </summary>

    public class RemoteLoaderFactory : MarshalByRefObject

    {

        private const BindingFlags bfi = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;

 

        public RemoteLoaderFactory() { }

 

        /// <summary> Factory method to create an instance of the type whose name is specified,

        /// using the named assembly file and the constructor that best matches the specified parameters. </summary>

        /// <param name="assemblyFile"> The name of a file that contains an assembly where the type named typeName is sought. </param>

        /// <param name="typeName"> The name of the preferred type. </param>

        /// <param name="constructArgs"> An array of arguments that match in number, order, and type the parameters of the constructor to invoke, or null for default constructor. </param>

        /// <returns> The return value is the created object represented as ILiveInterface. </returns>

        public IRemoteInterface Create(string assemblyFile, string typeName, params object[] constructArgs)

        {

            try

            {

                return (IRemoteInterface)Activator.CreateInstanceFrom(

                    assemblyFile, typeName, false, bfi, null, constructArgs,

                    null, null, null).Unwrap();

            }

            catch(Exception ex)

            {

                EventLog.LogError(ex.ToString(), "RemoteLoaderCS " + "RemoteLoaderFactory Create");

                return null;

            }

        }

    }

}

 

2. Load DLL In a New Application Domain


 

RemoteLoaderCS.RemoteLoaderFactory myFactory = null;

AppDomain myAppDomain = null;

string myNamespace = null;

private bool DynamicLink()

{

    try

    {

        // ** Create an AppDomain

        AppDomainSetup loSetup = new AppDomainSetup();

        loSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

        myAppDomain = "YouDLLNameAndLocation.dll";

        // create the factory class in the secondary app-domain      

        myFactory =

            (RemoteLoaderCS.RemoteLoaderFactory)myAppDomain.CreateInstance("RemoteLoaderCS",

            "RemoteLoaderCS.RemoteLoaderFactory").Unwrap();

        foreach (MyObject obj in myView.Document)

        {

            if (obj is PetriNetNode)

            {

                if (!LoadDObjects(obj as PetriNetNode))

                {

                    MessageBox.Show("Couldn't load a Dynamic class library.");

                    myFactory = null;

                    IsRunning = false;

                    AppDomain.Unload(myAppDomain);

                    return false;

                }

            }

        }

        IsRunning = true;

        return IsRunning;

    }

 

    catch (Exception ex)

    {

        return false;

    }

}

 

 

Additional Sources you can consider:

Run-Time Code Generation 1

Run-Time Code Generation 2

Dynamically executing code in .Net

Loading and Unloading an Assembly at Runtime