Reputation: 1438
Let me preface this question by saying I am fairly new to the concept of Assemblies. I am trying to create a method with in a namespace I have called API. The methods look like this:
Partial Public Class AppInfo
' This function will return the VersionMajor Element of the Assembly Version
Function VersionMajor() As String
Dim txt As String = Assembly.GetExecutingAssembly.GetName.Version.Major.ToString()
If txt.Length > 0 Then
Return txt
Else
Return String.Empty
End If
End Function
' This function will return the VersionMinor Element of the Assembly Version
Function VersionMinor() As String
Dim txt As String = Assembly.GetExecutingAssembly.GetName.Version.Minor.ToString()
If txt.Length > 0 Then
Return txt
Else
Return String.Empty
End If
End Function
' This function will return the VersionPatch Element of the Assembly Version
Function VersionPatch() As String
Dim txt As String = Assembly.GetExecutingAssembly().GetName().Version.Build.ToString()
If txt.Length > 0 Then
Return txt
Else
Return String.Empty
End If
End Function
' This function will return the entire Version Number of the Assembly Version
Function Version() As String
Dim Func As New AppInfo
Dim txt As String = VersionMajor() + "." + VersionMinor() + "." + VersionPatch()
If txt.Length > 0 Then
Return txt
Else
Return String.Empty
End If
End Function
End Class
I have other projects within the same solution that call the API as an additional reference. What I'd like to accomplish is say I have a project that references the API project called Test. In the home controller of test I have a view data that calls the Version method. Like this:
Function Index() As ActionResult
Dim func As New API.AppInfo
ViewData("1") = func.Version
Return View()
End Function
I'd like the viewdata to return the Version number of the Test assembly, but instead this returns the API Assembly version. What am I doing wrong here?
Upvotes: 1
Views: 2649
Reputation: 16878
According to MSDN, Assembly.GetExecutingAssembly
:
Gets the assembly that contains the code that is currently executing.
and it is always API assembly, because it is place when AppInfo.Version
is defined and executed.
What you want is to get information about calling assembly, meaning assembly that called your function AppInfo.Version
. You can get it by similar method Assembly.GetCallingAssembly
:
Returns the Assembly of the method that invoked the currently executing method.
Note: In your code Version
is calling VersionPatch
etc. internally which results in internal assembly call. It would be better that Version
use GetCallingAssembly
directly.
Note 2: Read carefully about method inlinig in the GetCallingAssembly
documentation provided above and decorate your Version
method with MethodImplOptions.NoInlining
attribute to avoid inlining.
Upvotes: 2