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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:20:45+00:00 2026-06-08T21:20:45+00:00

I know C# code can be compiled at runtime using C# . However I’m

  • 0

I know C# code can be compiled at runtime using C#. However I’m very very shaky at it since I just read about it a few minutes ago. I learn a lot better by examples. So tell me. If I want to compile something like:

// MapScript.CS
String[] LevelMap = {
"WWWWWWWWWWWWWWWWWWW",
"WGGGGGGGGGGGGGGGGGW",
"WGGGGGGGGGGGGGGGGGW",
"WWWWWWWWWWWWWWWWWWW" };

and use this array in my code, how would I go about it?

In pseudocode I want to do something like this:

Open("MapScript.CS");
String[] levelMap = CompileArray("levelMap");
// use the array
  • 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-08T21:20:47+00:00Added an answer on June 8, 2026 at 9:20 pm

    LINQ Expression trees are probably the friendliest way of doing this: Perhaps something like:

    • http://msdn.microsoft.com/en-us/library/system.linq.expressions.newarrayexpression.aspx
    • http://msdn.microsoft.com/en-us/library/bb357903.aspx

    You can also generate the IL using OpCodes (OpCodes.Newarr). Easy if you are comfortable with stack-based programming (otherwise, can be challenging).

    Lastly, you can use the CodeDom (which your pseudocode resembles), but–while the most powerful tool–it is less ideal for quick dynamic methods. It requires file system permissions and manual reference resolution since you are working closely with the compiler.

    Sample from MSDN

    var ca1 = new CodeArrayCreateExpression("System.Int32", 10);                        
    var cv1 = new CodeVariableDeclarationStatement("System.Int32[]", "x", ca1);
    

    Source – Creating Arrays with the Code DOM

    If you want a straight up raw compile of a string, you can omit the object-oriented treatment of the statements and instead just build a big string. Something like:

    var csc = new CSharpCodeProvider( new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } } );
    var cp = new CompilerParameters() {
        GenerateExecutable = false,
        OutputAssembly = outputAssemblyName,
        GenerateInMemory = true
    };
    
    cp.ReferencedAssemblies.Add( "mscorlib.dll" );
    cp.ReferencedAssemblies.Add( "System.dll" );
    cp.ReferencedAssemblies.Add( "System.Core.dll" );
    
    StringBuilder sb = new StringBuilder();
    
    // The string can contain any valid c# code, but remember to resolve your references
    
    sb.Append( "namespace Foo{" );
    sb.Append( "using System;" );
    sb.Append( "public static class MyClass{");
    
    // your specific scenario
    sb.Append( @"public static readonly string[] LevelMap = {
        ""WWWWWWWWWWWWWWWWWWW"",
        ""WGGGGGGGGGGGGGGGGGW"",
        ""WGGGGGGGGGGGGGGGGGW"",
        ""WWWWWWWWWWWWWWWWWWW"" };" );
    
    sb.Append( "}}" );
    
    // "results" will usually contain very detailed error messages
    var results = csc.CompileAssemblyFromSource( cp, sb.ToString() );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the question you can probably tell that I don't know much about code!
How can I know during runtime that my code threw a Warning? example try
I know I can’t compile my C# code using the default Platform Target :
I would like to know the code which can enable me to get a
From code how can we know which interfaces one class implements? Example: interface IDrink
With this code I can download the file but I should know the file
Does anyone know where the code (not commercial stuff) can be found for Metaphone
I know you can use source control software for source code, but can you
I know that i can enter/add new properties via code manually into partial classes
(Yes I know I can call Java code from Scala; but that is pointless;

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.