0

I'm creating a C# application in which code is compiled at runtime, the code is contained in a string (and the string gets its value from a ScintillaNET Control, it just returns text, the string with code is working as intended).

My question is: is there a way to make some sort of Class-object from this source code at runtime? For example, the string contains this value:

namespace _Testing {

class Program {
    static void Main(string[] args) {
        Console.Title = "Program";
        Console.WriteLine("If you can read this, it's all good!");
        Console.ReadKey();
    }
  }
}

This code is being compiled by my CSharpCodeProvider compiler at runtime (with a CompileAssemblyFromSourceBatch - because I'm passing an array of classes to be compiled). However, I want to be able to set the MainClass property of the compiler at runtime, and that requires getting the namespace out of the classes.

So I was thinking of creating some sort of object of each class-source code string which will make me able to achieve my goal. Any other ideas are of course welcome too.

Fluppe
  • 479
  • 7
  • 22
  • http://support2.microsoft.com/kb/304655 might help you. – Sjips Nov 24 '14 at 22:41
  • Yes you will want to Lean Roslyn. It's a dynamic compiler just for C#. Warning, it's a bit deep... – JWP Nov 24 '14 at 22:42
  • @Sjips, my current application already has a fully working compiling functionality, I have already consulted that article a few times in the beginning of my project ;). The only problem that remains is getting the namespace of the class out of the string source, so I'm trying to figure out a clean to achieve that – Fluppe Nov 24 '14 at 22:51

1 Answers1

0

What you want is not the CSharpCodeProver, for compiling and loading dynamic code. In your case i would recommend to use the DLR (not the CLR) from the .Net Framework.

Or you can take a look at .NET-Compiler-Plattform

What you try will not work.

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • Hmm, the Roslyn compiler seems a bit heavy for the small functionality I'm trying to add, do you have any other suggestions for getting the namespaces out of the classes that are to be compiled? That's basically the only thing I'm trying to do here, maybe my idea of creating objects of the source code strings is too far-fetched but I figured it was a clean way to do it – Fluppe Nov 24 '14 at 22:59