Get Framework Info
Retrieves the target .NET Framework version of a given assembly.
The function first attempts to read the TargetFrameworkAttribute to determine the framework. If the attribute is not present (e.g., in assemblies built before .NET 4.0), it falls back to inspecting the version of the referenced mscorlib.
VB
Public Function GetFrameworkInfo(Assembly As Assembly) As String
Dim frameWork As String = String.Empty
'Try to get TargetFrameworkAttribute
Dim attributes() As Object = Assembly.GetCustomAttributes(GetType(TargetFrameworkAttribute), False)
If attributes.Length > 0 Then
Dim tfa As TargetFrameworkAttribute = CType(attributes(0), TargetFrameworkAttribute)
frameWork = tfa.FrameworkName
Else
'For assemblies compiled with versions previous .NET 4.0, analyse the referenced mscorlib
Dim referencedAssemblies() As AssemblyName = Assembly.GetReferencedAssemblies()
Dim mscorlibRef As AssemblyName = referencedAssemblies.FirstOrDefault(Function(a) a.Name = "mscorlib")
If mscorlibRef IsNot Nothing Then
frameWork = mscorlibRef.Version.ToString()
End If
End If
Return frameWork.Replace(",", ", ").Replace("=v", " ").Replace(".NET", ".NET ")
End Function