Saturday, November 23, 2019

How to load an Assembly (DLL) programatically

Sample code to load Assembly. (DLL) programatically

Assembly assembly = Assembly.LoadFile(Properties.Settings.Default.AssemblyPath);

            Type myClassType =
            (from type in assembly.GetExportedTypes()
             where typeof(InterfaceBulkLoad).IsAssignableFrom(type)
             select type).Single();

            var instance = (InterfaceBulkLoad)Activator.CreateInstance(myClassType);
            if (args != null)
            {
                var isLoaded = instance.LoadFiles(new LoadFileRequest
                {
                    Environment = Properties.Settings.Default.Environment,
                    ProcessName = args[0],
                    ConnectionString = Properties.Settings.Default.ConnectionString
                });

                Console.WriteLine(isLoaded);
            }
            else
            {
                Console.WriteLine("Arguments were not supplied");
            }