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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:28:13+00:00 2026-05-19T23:28:13+00:00

Okay I will first admit I don’t know the proper terminology for all this

  • 0

Okay I will first admit I don’t know the proper terminology for all this so I apologize if this is addressed already and I just am not using the appropriate words.

I’m trying to make a program in C# that will allow C# code compiled by the program to act as a scripting language.

Here is my example code for the program that loads and compiles the code:

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace RuntimeCompilation
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Program b = new Program();
            CompilerResults results = b.BuildAssembly("Class1.cs"); // compiles the class

            if (results.Errors.Count > 0)
            {
                System.Console.Out.WriteLine("Errors Follow:");
                String ErrorText = "";
                foreach (CompilerError CompErr in results.Errors)
                {
                    ErrorText = ErrorText +
                        "Line number " + CompErr.Line +
                        ", Error Number: " + CompErr.ErrorNumber +
                        ", '" + CompErr.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
                }
                System.Console.Out.WriteLine(ErrorText);
            }
            else
            {
                Assembly assembly = results.CompiledAssembly;
                foreach (Type type in assembly.GetTypes())
                {
                    System.Console.WriteLine("Type found: " + type.FullName);
                    System.Console.WriteLine("Creating Instance:");
                    System.Console.WriteLine("=======================");
                    assembly.CreateInstance(type.FullName);
                    System.Console.Out.WriteLine("=======================");
                    System.Console.Out.WriteLine("Done");
                }
            }
            System.Console.Out.WriteLine("DONE.");
        }

        // Compile a file of source and return the CompilerResults object.
        private CompilerResults BuildAssembly(string filename)
        {
            TextReader tr = new StreamReader(filename);
            string code = tr.ReadToEnd();
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters compilerparams = new CompilerParameters();
            compilerparams.GenerateExecutable = false;
            compilerparams.GenerateInMemory = true;
            return provider.CompileAssemblyFromSource(compilerparams, code);
        }

        public bool iCanHasAccess()
        {
            return true;
        }
    }
}

And here is the class it builds:

using System;
using RuntimeCompilation;

namespace RuntimeCompilation
{
    public class Class1
    {
        public Class1()
        {
            System.Console.WriteLine("This was instancialized");
            if (Program.iCanHasAccess())
                System.Console.WriteLine("I have access");
        }
    }
}

The error outputted by the program is that the name Program doesn’t exist in the current context. How can I ensure that the “script” program has access to the functions and members of the compiling program?

I’m trying to allow an easy way with minimal runtime overhead to let users modify the behaviour of a component within the program without requiring a rebuild. If this is not possible through C# assemblies I’ll likely use LUA but I’d like to avoid it. As I’d like this product to work on an XBox360 necessitating compiling the WHOLE project and scripts together before deployment through XNA.

  • 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-05-19T23:28:13+00:00Added an answer on May 19, 2026 at 11:28 pm

    You’ll need a reference to the current assembly, i.e. something like:

    compilerparams.ReferencedAssemblies.Add("my.dll"); // or .exe
    

    however – the following will still fail:

            if (Program.iCanHasAccess())
                System.Console.WriteLine("I have access");
    

    because iCanHasAccess() is an instance method, not a static method.

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

Sidebar

Related Questions

Okay, so this will probably be closed or whatever, I don't care. I have
okay so I want to make a public function that will return YES if
Okay, so long weekend away with Macbook I started making an asset loader for
I'm really confused on how to go about doing this. I want to be
This case is really strange, i've spent 2 whole days to get Twitter Oauth
I've a custom property on a user control which has multiple state/modes. If this
I'm having trouble understanding why arrays in C# are covariant and what benefits this
I have a table of users profiles. Every user can have many profiles and
I am a little confused as to how passing pointers works. Let's say I
I'm trying to figure out the reason behind some regex comparison results I'm getting

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.