Cross Language References in ASP.NET
Most ASP.NET programmers are aware that the environment allows programmers to write code in multiple languages. This is what allows a programmer who prefers CSharp to write modules for DotNetNuke in CSharp even though the core code is written in VB.NET. (Until version 6.0, anyhow, where the core will finally be written in CSharp.)
What many don’t realize is that you can’t have CSharp code reference VB.NET and also have VB.NET code reference the CSharp code within the same application. At least, you can’t do it directly.
The project I’m working on is primarily written in VB.NET. It’s the 5.x version of DNN. Right before they moved the core to CSharp. All of my code is written in CSharp. So my code can reference the core without any trouble. This is because each language produces its own DLL and you can’t have two DLLs reference each other because that would cause a circular reference.
When you need to have DLLs reference each other, you need to use reflection from one of the two DLLs to reference the code in the DLL that can’t be accessed directly.
Most of the articles I found on the web stop with that explanation. Actually, most of them don’t even bother to mention that using reflection is part of the solution. The question I spent more time trying to find the answer to than I would like is, “How do I dynamically load an assembly that is produced from an App_Code subdirectory and that will have a somewhat random name?”
Here’s the line of code you need:
System.Reflection.Assembly.Load(<font class="str">"App_SubCode_"</font>+ appCodeSubDirectoryName)
This returns a System.Reflection.Assembly object and from there you can use reflection like you would anywhere else. If you need information on how to do that, I’ve written about that on other places on this site.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)