Hi All,
I've been working on having my application create dynamic assemblies
within the current domain of the application and loading them at a
later point inside new domains (so they can be unloaded as necessary)
and I feel I'm almost there thanks to great help from people in the
dotnet newsgroups.
I have, however, reached another impasse which is probably very simple
to solve but I'm just not seeing it. I'll try to explain:
My dynamic assemblies have types defined (TypeBuilder) which must
inherit from (early-bound) types already defined in the current domain.
Cracking open one of my dll's in ILDASM reveals that, in fact, the
dynamic types extend the types intended but I can't help thinking that
I should probably have some references to the appropriate assemblies to
actually make those extensions work.
I haven't been able to locate methods on either
System.Reflection.Emit.AssemblyBuilder or
System.Reflection.Emit.TypeBuilder that would allow you to add
references and am confused as to why I am missing this.
*All* suggestions are greatly appreciated :)
Thanks, Rein
Nick Hertl - 18 Oct 2005 16:25 GMT
With Reflection.Emit, you don't needto "reference" assemblies that are
already loaded because you already have them.
For example, let's say you had a type in one.dll and a type in two.dll.
A type in two.dll extends a type in one.dll. In order to compile
these using the C# compiler, you need to first do "csc /target:library
one.cs" and then "csc /target:library /r:one.dll two.cs"
However, if you want to use Reflection emit to create this "two"
assembly with a type that extends a type in one.dll, you can just load
make sure that one.dll is loaded, and if it's not, load it. Then use
the overload of ModuleBuilder.DefineType that takes a Type argument.
There are many ways to get that type from one.dll, but start with
Assembly.GetType(string) on the assembly that you know has that type in
it.