mirhagk
mirhagk

Reputation: 1285

How to add references using Reflection.Emit

I am writing a compiler for a language that runs on the .NET framework. I'm trying to generate code for an import statement. Basically

import System.Drawing

Should behave like

using System.Drawing;

in C#. The import statement will also support things like:

import Foo.Bar in "foo.dll"

Which will import the Foo.Bar class in foo.dll. My question is how would I go about linking these into my generated program using Reflection.Emit?

Thank you.

Upvotes: 2

Views: 1091

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063058

You just: consume the library. Meaning, you just use types / methods from foo.dll in your emitted code. The Reflection.Emit wrapper will add the necessary reference metadata automatically. Note that this means that if your code imports a library and then doesn't use it, then that reference will not exist in the generated IL. Which funnily enough is exactly what the C# compiler does too (and probably the VB one).

Upvotes: 3

Related Questions