Can I dynamically invoke an assembly of higher .Net-version from assembly of lower .Net version? Maybe I should use embedded DLR-language (Iron Python)?
2 Answers
An assembly doesn't have a .NET version, it has a metadata version. Ignoring early ones, there are three distinct ones in the wild. Respectively the versions that came out with .NET 1.1, .NET 2.0 and .NET 4. Intermediary releases, anything between 2.0 and 3.5 SP1, use the version 2.0 metadata format.
Or to put it another way, the CLR version is what really matters. Which is why @Barfieldmv's code works, .NET 2.0 and .NET 3.5 use the same CLR version. That runs out of gas on a more typical problem today, CLR version 2 cannot load a assembly that has version 4 metadata. You must run the program with the version 4 CLR. That requires a app.exe.config file that overrides the CLR version that will be used. It should look like this:
<configuration>
<startup>
<supportedRuntime version="v4.0"/>
</startup>
</configuration>

- 922,412
- 146
- 1,693
- 2,536
-
Great answer! But can you prompt me, is it possible to invoke .Net 4 assembly from .Net 2.0/3.5 assembly using some scripting engine? – macropas Jun 15 '11 at 11:08
-
No, there can only be one version of the CLR present in a process. The version 4 CLR has some workarounds for this restriction, but if you've got that CLR loaded then you wouldn't have this problem. – Hans Passant Jun 15 '11 at 11:17
Sure you can, this question should help you along the way:
Loading .net 3.5 wpf-forms in a .net 2.0 application
Or in code:
Dim dllPath As String = "C:\ProjectsTest\TestSolution\ActiveXUser\bin\Debug\TestControl.dll"
If Not File.Exists(dllPath) Then
Return
End If
Dim loadedAssembly As [Assembly] = [Assembly].LoadFile(dllPath)
Dim mytypes As Type() = loadedAssembly.GetTypes()
Dim t As Type = mytypes(1)
Dim obj As [Object] = Activator.CreateInstance(t)
TestControl.dll can contain all installed .net version information.

- 1
- 1

- 3,392
- 2
- 27
- 54
-
And how to invoke an arbitrary method from this object (e.g. static method)? – macropas Jun 15 '11 at 10:13
-