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

  • Home
  • SEARCH
  • 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 789683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:33:46+00:00 2026-05-14T21:33:46+00:00

I’m trying to figure out if it’s possible when you are dynamically generating assemblies,

  • 0

I’m trying to figure out if it’s possible when you are dynamically generating assemblies, to reference a type in a previously dynamically generated assembly.

For example:

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();

parameters.GenerateInMemory = true;

CompilerResults results = provider.CompileAssemblyFromSource(parameters, @"
namespace Dynamic
{
    public class A
    {
    }
}
");

Assembly assem = results.CompiledAssembly;

CodeDomProvider provider2 = new CSharpCodeProvider();
CompilerParameters parameters2 = new CompilerParameters();

parameters2.ReferencedAssemblies.Add(assem.FullName);
parameters2.GenerateInMemory = true;

CompilerResults results2 = provider2.CompileAssemblyFromSource(parameters2, @"
namespace Dynamic
{
    public class B : A
    {
    }
}
");

if (results2.Errors.HasErrors)
{
    foreach (CompilerError error in results2.Errors)
    {
        Console.WriteLine(error.ErrorText);
    }
}
else
{
    Assembly assem2 = results2.CompiledAssembly;
}

This code prints the following on the console: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)

I’ve tried it lots of different ways, but nothing seems to be working. Am I missing something? Is this even possible?

EDIT: Fixing the bug in the code provides this error instead:
Metadata file 'l0livsmn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' could not be found

EDIT2: Bit of a side note, but changing GenerateInMemory to false, and doing parameters2.ReferencedAssemblies.Add(assem.Location); will cause it to compile correctly, but I’d greatly prefer to reference the assembly that is directly in memory rather than outputting temporary files.

  • 1 1 Answer
  • 1 View
  • 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-05-14T21:33:46+00:00Added an answer on May 14, 2026 at 9:33 pm

    I think that in

    CompilerResults results2 = provider2.CompileAssemblyFromSource(parameters, @"
    namespace Dynamic
    {
        public class B : A
        {
        }
    }
    ");
    

    You want to pass parameters2, not parameters.

    I found the way to do it, you need NOT to compile the first one in memory, if you don’t do that, it will create a dll for this assembly in your temp directory, plus, in your call to

    ReferencedAssemblies.Add() 
    

    you dont pass the assembly name, you pass the assembly path, take a look at this code, it should work flawlessly :

            CodeDomProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();            
    
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, @"
                namespace Dynamic
                {
                    public class A
                    {
                    }
                }
                ");
    
            Assembly assem = results.CompiledAssembly;
    
            CodeDomProvider provider2 = new CSharpCodeProvider();
            CompilerParameters parameters2 = new CompilerParameters();
    
            parameters2.ReferencedAssemblies.Add(assem.Location);
            parameters2.GenerateInMemory = true;
    
            CompilerResults results2 = provider2.CompileAssemblyFromSource(parameters2, @"
                namespace Dynamic
                {
                    public class B : A
                    {
                    }
                }
                ");
    
            if (results2.Errors.HasErrors)
            {
                foreach (CompilerError error in results2.Errors)
                {
                    Console.WriteLine(error.ErrorText);
                }
            }
            else
            {
                Assembly assem2 = results2.CompiledAssembly;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.