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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:30:58+00:00 2026-06-08T03:30:58+00:00

When using a CodeCompileUnit to generate code via the CodeDOM, you can import namespaces

  • 0

When using a CodeCompileUnit to generate code via the CodeDOM, you can import namespaces by creating a CodeNamespace separate from the namespace in which you’re defining your type(s). Using the Imports property allows you to add the namespaces you desire. If you add the CodeNamespace to the CodeCompile unit, the imports will appear at the top of the file generated by the CodeDOM.

For instance, using a CSharpCodeProvider to compile the following CodeDOM graph:

CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace importsNamespace = new CodeNamespace {Imports = {new CodeNamespaceImport("System"), new CodeNamespaceImport("System.Collections.Generic"), new CodeNamespaceImport("System.Linq")}};
CodeNamespace typeNamespace = new CodeNamespace("MyTypeNamespace");
compileUnit.Namespaces.Add(importsNamespace);
compileUnit.Namespaces.Add(typeNamespace);

will generate

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyTypeNamespace {

}

The CodeSnippetCompileUnit allows you to compile type definitions which have been entered as a string. For instance, the string “public class TestClass {}” can be used as the Value for a CodeSnippetCompileUnit. Using the same code as above with a CodeSnippetCompileUnit:

const string testClassCodeString = "public class TestClass {}";
CodeSnippetCompileUnit snippetCompileUnit = new CodeSnippetCompileUnit {Value = testClassCodeString};
CodeNamespace importsNamespace = new CodeNamespace {Imports = {new CodeNamespaceImport("System"), new CodeNamespaceImport("System.Collections.Generic"), new CodeNamespaceImport("System.Linq")}};
CodeNamespace typeNamespace = new CodeNamespace("MyTypeNamespace");
compileUnit.Namespaces.Add(importsNamespace);
compileUnit.Namespaces.Add(typeNamespace);

will generate a file which contains only:

public class TestClass {}

How can you enclose this type in a namespace, or import namespaces to the file generated from a CodeSnippetCompileUnit?

  • 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-08T03:30:59+00:00Added an answer on June 8, 2026 at 3:30 am

    Even though CodeSnippetCompileUnit is derived from CodeCompileUnit, the compiler ignores the Namespaces property when generating code from the CodeDOM graph. Code generated from the CodeSnippetCompileUnit contains only the string that is in the Value property of the object.

    If you have a context-free code snippet, your only option to import namespaces or enclose the snippet in a namespace is to modify the string before setting the Value of the CodeSnippetCompileUnit.

    An example using the code in the question above as a starting place:

    string namespaceString = "MyTypeNamespace";
    string codeString = "public class TestClass {}";
    string snippetValue = string.Format(@"
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace {0}
    {{
    
    {1}
    
    }}", namespaceString, codeString, Environment.NewLine);
    
    var snippetCompileUnit = new CodeSnippetCompileUnit {Value = snippetValue};
    

    This will generate:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace MyTypeNamespace
    {
    
    public class TestClass {}
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've built some Types dynamically using System.CodeDom.CodeCompileUnit, want to compile those into IL code
I'm using CodeCompileUnit and CSharpCodeProvider to generate some source code. It adds the header
Using Delphi 2010. I am looking for (possibly) a function or procedure which can
I am dynamically generating some c# code using the CodeDom. I want to ad
Using Java, how can I extract all the links from a given web page?
Using DMD 2.057, I cannot get the following code to compile: import std.stdio; import
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using import datetime in python, is it possible to take a formatted time/date string

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.