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 9313605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:55:33+00:00 2026-06-19T01:55:33+00:00

I want to dynamically create assemblies in the integration tests for the purpose of

  • 0

I want to dynamically create assemblies in the integration tests for the purpose of testing some assembly manipulation classes. If I use the following code to create the testing assemblies:

var domain = AppDomain.CurrentDomain;

var builder = domain.DefineDynamicAssembly(
    new AssemblyName(assemblyName),
    AssemblyBuilderAccess.Save,
    directory);

builder.Save(fileName);

then everything runs ok, the assemblies are created in the required location, but as part of this they are also loaded to the current AppDomain, which I don’t want.

So I though about creating the assemblies using the separate AppDomain:

var domain = AppDomain.CreateDomain("Test");

...

But running the code throws an exception at the line var builder = domain.DefineDynamicAssembly(...);:

System.Runtime.Serialization.SerializationException: Type ‘System.Reflection.Emit.AssemblyBuilder’ in assembly ‘mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ is not marked as serializable.

I have no clue how this relates to calling DefineDynamicAssembly on a non-current AppDomain. All I find online is mostly about executing assemblies in separate domains. Perhaps what I’m trying to do here is just too specific, too advanced or even not recommended at all, but it would enable me to test all our assembly manipulation code.

Can someone please point me in the right direction?

  • 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-19T01:55:35+00:00Added an answer on June 19, 2026 at 1:55 am

    I got it working by executing the code in the other AppDomain, like suggested.

    var appdomain = AppDomain.CreateDomain("CreatingAssembliesAndExecutingTests", null,
       new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase });
    
    appdomain.DoCallBack(() =>
    {
       var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("temp"), AssemblyBuilderAccess.Run);
       var module = assembly.DefineDynamicModule("DynModule");
       var typeBuilder = module.DefineType("MyTempClass", TypeAttributes.Public | TypeAttributes.Serializable);
    });
    

    Remark you have to specify the ApplicationBase on the AppDomainSetup in order to find the delegate in the other AppDomain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use JavaCompiler to dynamically create some classes. I found the source
I want to dynamically create some image from Java and save it to a
i have an array of some values and i want to dynamically create textfields
I want to dynamically create classes at runtime in python. For example, I want
I'm trying to dynamically create objects out of forms, but I want some reduntant
I want to dynamically create a 1-row grid and add some TextBlocks to it,
I want to dynamically create instance method of child class through class method of
I want to dynamically create object of HtmlDivElement in my jsf managed bean and
I want to dynamically create controls in my bean. I am using JSF 2.0
I want to be able to dynamically create ViewControllers based on a JSON file.

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.