Load the given assembly. - CSharp System.Reflection

CSharp examples for System.Reflection:Assembly

Description

Load the given assembly.

Demo Code

// This program is free software; you can redistribute it and/or
using System.Diagnostics;
using System.Reflection;
using System.IO;//from  w  ww.  ja v  a 2 s.c  o  m
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Load the given assembly.
        /// </summary>
        /// <param name="dllName"></param>
        /// <returns></returns>
        public static Assembly LoadAssembly (string dllName) {
            Assembly sharpcvslib = null;
            try {
                // initial search location.
                DirectoryInfo baseDir = 
                    new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
                ArrayList possibleDlls = new ArrayList();
                possibleDlls.Add(
                    new FileInfo(Path.Combine(baseDir.FullName, dllName + ".dll")));

                // add some other cantidates
                DirectorySearch(possibleDlls, baseDir, dllName);

                foreach (FileInfo file in possibleDlls) {
                    try {
                        sharpcvslib = Assembly.LoadFrom(file.FullName);
                        AppDomain.CurrentDomain.AppendPrivatePath(Path.GetDirectoryName(sharpcvslib.Location));
                        break;
                    } catch (Exception) {
                        // keep on going
                    }
                }
            } catch (Exception) {
                // unable to load all assemblies, exit.
                System.Environment.Exit(-1);
            }
            return sharpcvslib;
        }
}

Related Tutorials