1
votes

I am wondering if it is possible to load an Assembly into an AppDomain using only the namespace string, for example System.Data.Odbc (without the rest of the fully qualified Assembly name)

I also cannot load the assembly using the path to the DLL, I need .NET to resolve this for me.

Background:

I have a C# script compiler running in another AppDomain, these scripts can contain references such as: System.Data.Odbc.OdbcConnection conn and I need to load types the script references.

I am parsing out System.Data.Odbc.OdbcConnection and loading System.Data.Odbc. I expect the loading of System.Data.Odbc.OdbcConnection to fail, and my code accounts for this, but I need to know if it's possible to load an assembly ONLY using the string System.Data.Odbc

I reviewed this question:

Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

However it does not help me, since it assumes knowledge of the dll file name.

It is safe to assume for this question that the libraries are located in the GAC or the app's base directory.

1
Namespaces and assembly names are entirely orthogonal - for example, System.Linq is mostly implemented in `System.Core.dll. Now, are you really talking about assembly names without version numbers, or about namespaces?Jon Skeet
Thanks Jon, I think I have some terminology mixed up. Basically my issue is: How can I load the assemblies that would resolve a dependency on System.Data.Odbc when I only have the string System.Data.Odbcdmck

1 Answers

4
votes

In general this is not possible to do. There is no inherent relationship between a DLL and a namespace. It's possible for multiple DLL's to contain the same namespace and a single DLL to contain multiple different root namespaces. So there is not guaranteed to be a 1-1 mapping here.

However you could create a table in your application between a given namespace and the set of DLLs which do contain it. Then for a namespace try and load all of those DLL's in sequence. That will get you close to the effect you are looking for (but only for a limited set of available DLLs)