my goal is to write a class that can compile/serialize any piece of code. I am thinking of some ideas and it's just a concept, I don't expect you to write entire solution, I am thinking of best way to do that. I thought about two ideas:
- Create an interface in compiler class, then read interface method body and compile it into separate assembly using roslyn.
- Serialize expression tree.
AD 1. First concept
public class Runnable : IRunnable
{
    void Run();
}
public interface ICodeRunner
{
    void RunCode<T>() where T : IRunnable
}
public class CodeRunner : ICodeRunner
{
    public void RunCode<T>() where T : IRunnable
    {
        MethodInfo mi = typeof(T).GetMethod("Run");
        MethodBody mb = mi.GetMethodBody();
        // somehow get method body if possible and compile it using roslyn.
        // like this:
        // string syntaxTree = mb.GetMethodBodyToString() // this method doesn't exist. There must be a way to do that
        // CSharpCompilation compilation = CSharpCompilation.Create(
        //    "abc",
        //    new[] { syntaxTree },
        //    new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) },
        //    new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
        // using (var dllStream = new FileStream(@"abc.dll", FileMode.OpenOrCreate))
        // using (var pdbStream = new FileStream(@"abc.pdb", FileMode.OpenOrCreate))
        // {
        //    var emitResult = compilation.Emit(dllStream, pdbStream);
        //    if (!emitResult.Success)
        //    {
        //        // emitResult.Diagnostics
        //    }
        // }
    }
}
The alternative is to serialize lambda as expression tree into a file, I saw some libraries to do that.
Are there any easier ways to this?
My general use case is to create a class that can serialize some code (logic) and payload into two separate files, send it over the wire and run piece of code + payload on a different machine. The interface is exe file and input file which is sent over the wire. Are there any ready solution for this problem?
 
    