开发者

Getting metadata for compiling .net language

I'm toying with doing a proof of concept for a new language targeting the .Net platform. I've got the lexing/parser thing pretty much sorted. Lazy as I am I'm simply going to generate C# using the CodeDom rather than emitting IL at this point and then just compile that.

However, in order to generate the correct C# for my syntax I need to be able to have all the metadata of referenced assemblies available for the "compiler" so I can look up all the classes, methods, parameters, interfaces etc. What's the best way to go about this.

I guess I could load all the assemblies into their own appdomain and query them through reflection but it seems a bit clunky. Another way I guess would be to extract all metadata into something that can be loaded and queried easily and performantly.

Another way would be to load the XML metadata files from the system .Net framework directory but that seems a bit clunky as well

It seems this should be a problem that the default compiler itself would address, am I missing something o开发者_开发知识库bvious way to do this?

EDIT

This CCI metadata might be the way to do it, but still curious how it's done by the compiler


The C# and VB compilers have an internal library that reads the metadata out of the PE format and interprets it in it's raw form. I think many of the managed libraries do the same thing -- the metdata format is open and well documented (just search for the ECMA CLI spec), it's safer than going through the CLR (since the code is read as bits and not loaded into a runtime) and generally faster.

If/when the teams finish on their discussed "compiler as a service" long-term plans, there might be something that's actually available directly from the compiler team, but that's: a) going to be at some vague and undetermined time in the future, and b) purely speculation on my part. So for now, I'd look at some of the libraries that other people are pointing to.


You can try Assembly.ReflectionOnlyLoad. This will only load the requested assembly, without loading dependencies. However, unloading is still impossible (without unloading a whole appdomain).


The best way for fully managed libraries would be to use Cecil as described on the project page :

In simple English, with Cecil, you can load existing managed assemblies, browse all the contained types, modify them on the fly and save back to the disk the modified assembly.

It's maintained, open source and the license make it usable even in commercial projects.


Compilers will typically load assemblies like any other data file (using something like the Cecil or CCI metadata libraries mentioned already). It's faster and uses less memory than the runtime's reflection support.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜