Jon
Wed Jun 06 16:34:31 CDT 2007
Barry Kelly <barry.j.kelly@gmail.com> wrote:
> > 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.
Well, that's *one* use for it. I certainly use it to compile code which
I've already got as C#. It certainly makes that pretty easy.
> 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.
Well, I was going on the basis of: "If you mean that this external
source is written in C#, I think you have your work cut out for you."
In fact, if the source is written in C#, it's really pretty easy.
> 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.
It's possible you've missed the compilation side of CSharpCodeProvider
- in particular, CompileAssemblyFromSource, CompileAssemblyFromFile
etc.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too