-3

How to load a component from an Assembly without loading the complete Assembly in the memory?

Say if I have a UserControl UC1 in an Assembly, I want to load it either in XAML or c# code without loading the whole assembly file? Can I do it?

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66

2 Answers2

1

For code inspection only, you need to read this article on MSDN. With that approach, inspected library is not loaded. Unfortunatelly, to execute method, you need to load assembly and methods from assembly as other posts are sugesting.

Pavel Pája Halbich
  • 1,529
  • 2
  • 17
  • 22
0

From other stack overflow post: Loading DLLs at runtime in C#

Members must be resolvable at compile time to be called directly from C#. Otherwise you must use reflection or dynamic objects.

namespace ConsoleApplication1 {
    using System;
    using System.Reflection;

    class Program
    {
        static void Main(string[] args)
        {
            var DLL = Assembly.LoadFile(@"C:\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\DLL.dll");

            foreach(Type type in DLL.GetExportedTypes())
            {
                var c = Activator.CreateInstance(type);
                type.InvokeMember("Output", BindingFlags.InvokeMethod, null, c, new object[] {@"Hello"});
            }

            Console.ReadLine();
        }
    }

}

Community
  • 1
  • 1
Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34