开发者

Dynamic invoking of arbitrary .Net-version assembly

Can I dynamically invoke an assembly of higher .Net-version from assembly of lower .Net version? 开发者_JAVA百科Maybe I should use embedded DLR-language (Iron Python)?


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜