How to get assembly file info?

B

Brett

Using Reflector,
URL:http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector.zip, I
see in System.Reflection there is a class called AssemblyTitleAttribute. It
has a read only property named Title. I do the following to get my
application name.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim apptitle As System.Reflection.AssemblyTitleAttribute

Me.Label1.Text = apptitle.Title.ToString
End Sub

Error is:

An unhandled exception of type 'System.NullReferenceException' occurred in
MainForm.exe

Additional information: Object reference not set to an instance of an
object.


However, if I try to declare an instance:
Dim apptitle As New System.Reflection.AssemblyTitleAttribute

I get this error under apptitle:
Argument not specified for parameter 'title' of Public Sub New(Title As
String)

Can I use the above class to get the title? There's a work around available
to getting assembly file info but it requires a lot of code. I'd like to
understand what I have done wrong here though.

Thanks,
Brett
 
O

Oenone

Brett said:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim apptitle As System.Reflection.AssemblyTitleAttribute

Me.Label1.Text = apptitle.Title.ToString
End Sub

The problem here is that you declared an AssemblyTitleAttribute object, but
you haven't set it to anything, so you just have a pointer to Nothing.
Dim apptitle As New System.Reflection.AssemblyTitleAttribute

Here you're trying to create a brand new AssemblyTitleAttribute. That's not
going to help you, as it won't have your assembly's title stored within it.

Try this:

Imports System.Reflection
[...]

Dim objTitle As AssemblyTitleAttribute
objTitle = DirectCast( _
AssemblyTitleAttribute.GetCustomAttribute( _
Me.GetType.Assembly, GetType(AssemblyTitleAttribute)), _
AssemblyTitleAttribute)
Me.Label1.Text = objTitle.Title

This declares an AssemblyTitleAttribute object, as in your first piece of
code, which initially points to Nothing.

It then uses the shared GetCustomAttribute member of the
AssemblyTitleAttribute class to point the object at the
AssemblyTitleAttribute object that is built into your project. It uses
DirectCast to cast this from the generic Object to the specific object type
being used (AssemblyTitleAttribute).

Once this has been done you can just read the Title string from the object.

Hope that helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top