Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:52:02+00:00 2026-06-15T08:52:02+00:00

I’m trying to load assemblies into another appdomain (to be able to unload them

  • 0

I’m trying to load assemblies into another appdomain (to be able to unload them when not needed) but also I want to be able to inspect types in the loaded assembly and create instances of my types (via Activator). Also I don’t want the assemblies to get locked when loaded.

I don’t see how this is possible. Things I have tried:

  • Loading Assembly.Load(File.ReadAllBytes(path)) helps me load an assembly without locking it, so it can get deleted/moved. This loads assembly into the current appdomain, so it won’t be possible to unload it.

  • Creating another AppDomain and using AppDomain.Load() loads the assembly into a new AppDomain, but it is not possible to check all the types that are in the loaded AppDomain. I can’t create anything, unless I know type fully qualified type name, and even then they have to either be Serializable or derive from MarshallByRef. Another AppDomain stuff just works via Proxies so it is harder to create things without having a Contract/Shared Interface, etc.

  • MEF also loads assemblies into the current AppDomain, so it is basically doing the same thing.

  • Mono.Cecil allows type inspection on a loaded assembly, but then I can’t create types using TypeReference. Maybe if there was a way to convert TypeReference to Type?

I have checked how ILSpy does this, and to no surpirse it uses Mono.Cecil to load/unload the assembly, but then again, it does not create instances of any types, just does the type inspection which is possible going via Mono.Cecil route.

Now the question is, is this even possible? Am I missing anything?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:52:04+00:00Added an answer on June 15, 2026 at 8:52 am

    If you load the assembly into another appdomain then you should of course create instances there and use them there. Other than that, not sure what problem you have run into.

    Updates

    1. added code to pick type from list
    2. changed to only pass type names across the domains as strings

    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.Remoting;
    
    public class MainClass : MarshalByRefObject
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("usage: {0} assembly", Path.GetFileName(Assembly.GetExecutingAssembly().Location));
                return;
            }
            AppDomain other = AppDomain.CreateDomain("other");
            Type myType = typeof(MainClass);
            // create a loader instance in the new domain
            MainClass loader = (MainClass)other.CreateInstanceAndUnwrap(myType.Assembly.FullName, myType.FullName);
            string assemblyName;
            string[] types = loader.LoadAssembly(args[0], out assemblyName);
            Console.WriteLine("Loaded assembly {0}.", assemblyName);
            Console.WriteLine("Types:");
            for(int i = 0; i < types.Length; i += 1)
            {
                Console.WriteLine("[{0}] {1}", i, types[i]);
            }
            Console.Write("Enter index of type to create, -1 to exit: ");
            int create = Int32.Parse(Console.ReadLine());
            if (create < 0)
            {
                return;
            }
            Console.WriteLine("Creating instance of type {0}", types[create]);
            Console.WriteLine("Type of the created instance was: {0}", loader.CreateInstance(assemblyName, types[create]));
        }
    
        string[] LoadAssembly(string path, out string assemblyName)
        {
            Console.WriteLine("LoadAssembly executing in appdomain {0}", AppDomain.CurrentDomain.FriendlyName);
            Assembly assembly = Assembly.Load(File.ReadAllBytes(path));
            assemblyName = assembly.FullName;
            return Array.ConvertAll<Type, string>(assembly.GetExportedTypes(), x => x.FullName);
        }
    
        string CreateInstance(string assemblyName, string typeName)
        {
            Console.WriteLine("CreateInstance executing in appdomain {0}", AppDomain.CurrentDomain.FriendlyName);
            object instance = Activator.CreateInstance(assemblyName, typeName).Unwrap();
            // do something useful with the instance here
            return instance.GetType().FullName;
        }
    
        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
    

    Sample output for log4net.dll:

    $ mono main.exe log4net.dll
    LoadAssembly executing in appdomain other
    Loaded assembly log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=a5715cc6d5c3540b.
    Types:
    [0] log4net.Core.SecurityContext
    [1] log4net.Core.LoggerWrapperImpl
    [2] log4net.Core.LogImpl
    [3] log4net.Core.DefaultRepositorySelector
    ...
    [160] log4net.Appender.AspNetTraceAppender
    [161] log4net.Appender.FileAppender
    ...
    [197] log4net.Repository.LoggerRepositoryConfigurationResetEventHandler
    [198] log4net.Repository.LoggerRepositoryConfigurationChangedEventHandler
    Enter index of type to create, -1 to exit: 161
    Creating instance of type log4net.Appender.FileAppender
    CreateInstance executing in appdomain other
    Type of the created instance was: log4net.Appender.FileAppender
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.