Add class to compiled assembly (in memory)

Since CompileAssemblyFromSource add custom functions in a smart way was ignored im going to ask this question differently so people will bother to read it.

cutting at the chase,i am making a language by "translating" the new syntax into c# and compiling it in memory in this fashion.

using (Microsoft.CSharp.CSharpCodeProvider CodeProv =
    new Microsoft.CSharp.CSharpCodeProvider())
    {
        CompilerResults results = CodeProv.CompileAssemblyFromSource(
             new System.CodeDom.Compiler.CompilerParameters()
             {
                 GenerateInMemory = true
             },
             code);

         var type = results.CompiledAssembly.GetType("MainClass");

         var obj = Activator.CreateInstance(type);

         var output = type.GetMethod("Execute").Invoke(obj, new object[] { });

         Console.WriteLine(output);
    }

basically i am executing a "main" function written inside the code variable. and i am using some functions in the code variable i would like to include without adding it as a string at the bottom like this:

        code += @"public void Write(string path, object thevar)
        {
        if (thevar.GetType() == typeof(string))
        {
        System.IO.File.WriteAllText(path,(string)thevar);
        }
        if (thevar.GetType() == typeof(string[]))
        {
        System.IO.File.WriteAllLines(path,(string[])thevar);
        }
        }";

Can i somehow add a class from my Actual main project in VS and let the compiled in memory code access it? without adding it as a string.

Answers


You can embed your source code file(s) as resources. With this technique you can edit the file in Visual Studio and access the contents of the files as if it was a string during run-time.

This link shows how to do it: https://stackoverflow.com/a/433182/540832


Need Your Help

ExtJS: What's the point of requires?

javascript extjs extjs4 extjs4.1 extjs-mvc

I've been trying to figure out what requires does in Ext JS 4, and I can't seem to come up with a reasonable answer. Let's say I have the following code: