This question already has an answer here:
Context
So recently I've worked on a little meta c# project by myself to get good at reflection. I wrote some basic methods that take text input and check if there's an appropriate Type/Method/Ctor in any of the loaded assemblies.
Those methods let me have a UI in which I can:
- Write a type name;
- Select one of that type's ctors or methods;
- Select which object to use as parameters and/or input literals;
- Save the returned object (if any) for later use in other methods.
Now this is all fine but then the next step of my project is to be able to do something similar but with methods, as in write text in UI, pick the available code "blocks" I can add to my method, and actually save it for later use.
Question
Is there any way to go from text to compiled c# code ?
My thoughts/knowledge
- ILDASM will give me the IL of compiled code.
- I can use Relfection.Emit to recreate that IL.
However, I obviously don't have compiled code since the starting point is text input (lets assume the text code would compile with no errors).
I was thinking maybe it's possible to parse the text with some parser (maybe Roslyn or any other tool for that), and somehow map the expression tree to OpCodes and Builders to use with Emit(). I sadly don't know how much effort/time that would require, so if you could enlighten me on the main challenges of such a task, that'd be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire