[Assembly].Load() ignores Version= ?

IfYouSaySo

Well-known member
Joined
Jan 4, 2006
Messages
102
Location
Hermosa Beach, CA
Programming Experience
3-5
OK...So I'm trying to dynamically instantiate a VBCodeProvider, using the Reflection namespace, and I want to use the 2.0 version, specifically not the 1.1 version, because I want to dynamically compile a bit of code that is using new .NET framework 2.0 features. It's not working for me. It seems as if the Version= part is ignored, because I can put in there whatever I want, and it doesn't complain:

VB.NET:
    Public Function GetProvider(ByVal lang As String) As CodeDomProvider
        Dim cfg As NameValueCollection = CType(ConfigurationSettings.GetConfig(lang.ToLower()), NameValueCollection)
        If cfg Is Nothing Then
            Throw New UnsupportedLanguageException(lang.ToLower())
        End If
        ' Load config settings
        Dim strAssembly As String = CType(cfg("Assembly"),[String])
        Dim strProvider As String = CType(cfg("Provider"),[String])
        Dim strVersion As String = CType(cfg("Version"),[String])
        Dim strPublicKeyToken As String = CType(cfg("PublicKeyToken"),[String])
        Dim strCulture As String = CType(cfg("Culture"),[String])
        If strCulture Is Nothing Then
            strCulture = "neutral"
        End If
        Dim str As String = strAssembly & ", Version=" & strVersion & ", Culture=" & strCulture & ", PublicKeyToken=" & strPublicKeyToken
        If strAssembly Is Nothing Or strProvider Is Nothing Or _
           strVersion Is Nothing Or strPublicKeyToken Is Nothing Then
           Throw New UnsupportedLanguageException(lang.ToLower())
        End If
        Dim asm As [Assembly] = [Assembly].Load(str)
        If asm Is Nothing Then
            Throw New InvalidAssemblyException(asm.ToString())
        End If
 
        Dim p As CodeDomProvider = CType(asm.CreateInstance(strProvider,True), CodeDomProvider)
        If p Is Nothing Then
            Throw New InvalidCodeProviderException(strProvider)
        End If
        Return p
    End Function

Now, the code above is compiled into a .NET 1.1 executable, but the .config file is provides settings that are explicitly calling out "System, version=2.0.0.0, culture=neutral,PublicKeyToken=b77a5c561934e089",
(which is what is in my GAC).

So now I'm wondering if dynamically instantiating a 2.0 object from a 1.1 executable is even possible, or if the version is ignored (informational only), or what...
 
.Net 2.0 assemblies are not supported with .Net 1.x application. An option is to make the .Net 2.0 library a common COM library that can be used by any windows programming platform, but then you are getting out of the managed .Net world.
 
What if I compile my App to be 2.0, and then I try to instantiate a 1.1 assembly, does that work? Not sure if I want to get into that though, because then it requires all my users to have 2.0 framework installed, and I'm not sure how prevalent that is yet.

As a side note, I'm not sure I understand why a 2.0 assembly couldn't be dynamically instantiated from a 1.1 executable. Did the IL change, or some other big change that would break compatibility? Do you know if there is a MSDN article that explains the details?

Thanks.
 
You can reference older .Net libraries from .Net2, but not the other way around. Any .Net2 assembly will require .Net2 framework to be installed in client.

Without explaining from IL, it simply don't work, try to add reference to .Net2 library from .Net1 application and you will get an error message. It is also logical since .Net2 class library is much larger than .Net1. There are lots of new .Net2 classes/properties/methods that don't exist in .Net1. If you were to code a .Net2 assembly with only classes supported by .Net1 you might as well do it in .Net1 studio. The .Net2 studio is also too tightly integrated with new features of the framework to disregard the platform it was built for (most settings, ClickOnce, designer partial classes, custom intellisense, snippets, MY, wizards, etc...).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/interopdotnet.asp
 
OK...

But what I'm trying to do is really much much different than that, and as far as I can see, only depends on the IL being compatible.

Because I have a program that uses the compilation features of the framework, so that it can compile an in-memory image from a source file and run it on the spot (inspired by reading about nscript on codeproject). And the compiler itself, is configurable via a .config file. This was built on the 1.1 framework.

Now, if I put a reference to the vb 2.0 compiler in my config file, and then provide a source file to compile which uses 2.0 features, what is the problem with that (bearing in mind that both 1.1 and 2.0 are installed, and both assemblies are in the GAC)? Keep in mind that I'm not seeing a failure of any kind--it's just silently loading the vb 1.1 compiler regardless of which assembly version I reference in my .config file. And then the compilation of course fails.

My question is, if I just give Assembly.Load a string that references a 2.0 assembly, shouldn't it either load it (regardless of version, because loading and dynamically running an assembly only depends on the ability to interpret IL as far as I can see), OR give an Exception. But loading the wrong thing doesn't seem like it should be an option.
 
I don't see how it is anything different at all, if you're running in a .Net1 assembly context you can't load a .Net2 assembly into it, not when the assembly initially loads and not later. The designer referenced assemblies are loaded when app is first starts, you try to dynamically load some more during runtime with reflection. .Net2 inside a .Net1 process is not supported, I don't know why it loads another version when there also exist one compatible of .Net1, but if you try to load a .Net2 class library you made yourself (no need to GAC) a BadImageFormatException is thrown.
 
Back
Top