Jon said:
Any reason you'd suggest not using the CSharpCodeProvider class? Easier
than calling csc, IMO.
Well, the code providers are targeted towards a DOM model for source
code. More specifically, they support designers that need to write code,
such as the WinForms designer. There's no deep semantic support - cf.
CodeSnippetExpression, not exactly type-safe. So, if the object is to
write code that writes C# code, then sure, CSharpCodeProvider might be
the way to go.
It depends on what's supposed to be in those source files on disk. It
could be viewed as rather pointless to parse some arbitrary language,
only to convert it into C# via a code provider, and then call the C#
compiler, and then try to load it up into a dynamic method (if that is
the goal) - or alternatively in a separate type, perhaps loaded in a
separate domain to get the unloadable / GC requirement in there as well.
If the target is to get a lightweight DynamicMethod that can work with
instances of a specific type, then I think CSharpCodeProvider is helping
with the wrong end of the problem.
It comes down to what's in those source files. If it's C#-like, I think
a C# text-replacement template would be easier than working it through a
code provider only to turn it back into C# again. If it's more
declarative or otherwise limited in scope, then I think more direct
translation into Reflection.Emit may be in order.
-- Barry