Creating an About Dialog with Assembly Attributes

About dialogs are pretty common. In this section I will demonstrate how to use Reflection to read assembly-level attributes (Listing 5.4), and then you can imagine how those capabilities might have been used to create the FileVersionInfo class. The FileVersionInfo class was used to create the About dialog in Figure 5.2, and the code is provided in Listing 5.5.

Listing 5.4 Obtaining Application Information by Using Reflection
 1:  Option Explicit On 2:  Option Strict On 3: 4:  Imports System.Reflection 5:  Imports System.Text.RegularExpressions 6:  Imports System.Type 7: 8:  Public Interface IVersionInformation 9: 10:   ReadOnly Property ProductName() As String 11:   ReadOnly Property ProductVersion() As String 12:   ReadOnly Property FileDescription() As String 13:   ReadOnly Property LegalCopyright() As String 14:   ReadOnly Property Comment() As String 15: 16: End Interface 17: 18: Public Interface IReflectedAttributes 19:   ReadOnly Property Attributes() As Object() 20: End Interface 21: 22: Public Class ReflectedVersionInformation 23: 24:   Implements IVersionInformation, IReflectedAttributes 25: 26:   Private FAttributes As Object() = Nothing 27: 28:   ReadOnly Property Attributes() As Object() _ 29:     Implements IReflectedAttributes.Attributes 30:   Get 31:     If (FAttributes Is Nothing) Then 32:       FAttributes = System.Reflection.Assembly.GetExecutingAssembly(). _ 33:         GetCustomAttributes(True) 34:     End If 35: 36:     Return FAttributes 37:   End Get 38:   End Property 39: 40:   Private ReadOnly Property GetAttribute( _ 41:     ByVal Name As String) As Attribute 42:   Get 43:     Dim A As Attribute 44:     For Each A In Attributes 45:       If (A.GetType().FullName = Name) Then 46:         Return A 47:       End If 48:     Next 49: 50:     Return Nothing 51:   End Get 52:   End Property 53: 54:   ReadOnly Property ProductName() As String _ 55:     Implements IVersionInformation.ProductName 56:   Get 57:     Try 58:       Return CType(GetAttribute( _ 59:         "System.Reflection.AssemblyProductAttribute"), _ 60:           AssemblyProductAttribute).Product 61:     Catch 62:       Return String.Empty 63:     End Try 64: 65:   End Get 66:   End Property 67: 68:   ReadOnly Property ProductVersion() As String _ 69:     Implements IVersionInformation.ProductVersion 70:   Get 71: 72:   End Get 73:   End Property 74: 75:   ReadOnly Property FileDescription() As String _ 76:     Implements IVersionInformation.FileDescription 77:   Get 78:     Try 79:       Return CType(GetAttribute( _ 80:         "System.Reflection.AssemblyTitleAttribute"), _ 81:         AssemblyTitleAttribute).Title 82:     Catch 83:       Return String.Empty 84:     End Try 85: 86:   End Get 87:   End Property 88: 89:   ReadOnly Property LegalCopyright() As String _ 90:     Implements IVersionInformation.LegalCopyright 91:   Get 92:     Try 93:       Return Regex.Unescape(CType(GetAttribute( _ 94:         "System.Reflection.AssemblyCopyrightAttribute"), _ 95:         AssemblyCopyrightAttribute).Copyright) 96:     Catch 97:       Return String.Empty 98:     End Try 99: 100:   End Get 101:   End Property 102: 103:   ReadOnly Property Comment() As String _ 104:     Implements IVersionInformation.Comment 105:   Get 106:     Try 107:       Return CType(GetAttribute( _ 108:         "System.Reflection.AssemblyDescriptionAttribute"), _ 109:         AssemblyDescriptionAttribute).Description 110: 111:     Catch 112:       Return String.Empty 113:     End Try 114:   End Get 115:   End Property 116: 117: End Class 
Figure 5.2. The About dialog example. (The graphic image was created using Bryce 5.)

graphics/05fig02.jpg

I combined a couple of techniques in this example. The ReflectedVersionInformation class created in lines 22 through 117 demonstrate how to obtain assembly-level information using Reflection. ReflectedVersionInformation shows you how to get the kind of information you might want to display in a dialog box. In addition to obtaining assembly-level information, the ReflectedVersionInformation class implements the IVersionInformation interface. Using this second technique I can implement my About dialog in terms of the IVersionInformation interface rather than a specific class, for example, instead of the ReflectedVersionInformation class.

NOTE

Caution : You cannot obtain the version information by reflecting the AssemblyVersionAttribute . The assembly version information is used to create an AssemblyQualifiedName , and the version number is embedded as part of that. As a result, the ReflectedVersionInformation class, as written, does not return the version number.

The second technique is referred to as interface programming . Using interfaces instead of classes makes it very easy to change an implementation simply by changing the instance of the class that implements a particular interface. Of course, Microsoft has already mitigated the need for reading file version information using Reflection attributes by implementing the FileVersionInfo class. Listing 5.5 defines the VersionInformation class and implements the IVersionInformation interface.

NOTE

I am not advocating using an interface to obtain file version information. If you want file version information, use the FileVersionInfo class. However, implementing interactions in terms of interfaces is a dynamic and flexible way to program as a general strategy. Refer to Listing 5.6 for an example of interface programming.

Listing 5.5 Creating an About Dialog by Using the FileVersionInfo Class
 1:  Option Explicit On 2:  Option Strict On 3: 4:  Imports System.Reflection 5:  Imports System.Text.RegularExpressions 6:  Imports System.Type 7: 8:  Public Interface IVersionInformationObject 9:    ReadOnly Property FileVersionInfo() As FileVersionInfo 10: End Interface 11: 12: Public Interface IVersionInformation 13: 14:   ReadOnly Property ProductName() As String 15:   ReadOnly Property ProductVersion() As String 16:   ReadOnly Property FileDescription() As String 17:   ReadOnly Property LegalCopyright() As String 18:   ReadOnly Property Comment() As String 19: 20: End Interface 21: 22: Public Class VersionInformation 23:   Implements IVersionInformation, IVersionInformationObject 24: 25:   Private FFileVersionInfo As FileVersionInfo 26: 27:   Public Sub New() 28:     FFileVersionInfo = _ 29:       FileVersionInfo.GetVersionInfo(Application.ExecutablePath) 30:   End Sub 31: 32:   ReadOnly Property FileVersionInfo() As FileVersionInfo _ 33:     Implements IVersionInformationObject.FileVersionInfo 34:   Get 35:     Return FFileVersionInfo 36:   End Get 37:   End Property 38: 39:   ReadOnly Property ProductName() As String _ 40:     Implements IVersionInformation.ProductName 41:   Get 42:     Return FFileVersionInfo.ProductName 43:   End Get 44:   End Property 45: 46:   ReadOnly Property ProductVersion() As String _ 47:     Implements IVersionInformation.ProductVersion 48:   Get 49:     Return "Version: " + FFileVersionInfo.ProductVersion 50:   End Get 51:   End Property 52: 53:   ReadOnly Property FileDescription() As String _ 54:     Implements IVersionInformation.FileDescription 55:   Get 56:     Return FFileVersionInfo.FileDescription 57:   End Get 58:   End Property 59: 60:   ReadOnly Property LegalCopyright() As String _ 61:     Implements IVersionInformation.LegalCopyright 62:   Get 63:     ' If we don't unescape then we get the literal text in VB 64:     Return Regex.Unescape(FFileVersionInfo.LegalCopyright) 65:   End Get 66:   End Property 67: 68:   ReadOnly Property Comment() As String _ 69:     Implements IVersionInformation.Comment 70:   Get 71:     Return FFileVersionInfo.Comments 72:   End Get 73:   End Property 74: End Class 

Listing 5.5 is straightforward. The VersionInformation class implements the IVersionInformation interface and returns the version information from the FileVersionInfo object.

The VersionInformation class demonstrates the assembly attributes that contain specific kinds of version information. For example, the FileVersionInfo.LegalCopyright property (lines 60 through 66) is used to contain copyright information. I used the literal value \xa9 to create the copyright symbol ( ). We then have to unescape the literal to correctly display the copyright symbol. The AssemblyCopyrightAttribute I used to create the output appears below.

 <Assembly: AssemblyCopyright("Copyright \xa9 2003. All Rights Reserved.")> 

Line 64 of Listing 5.5 shows how to unescape the literal string value to turn the literal text \xa9 to . Basic tricks like the one just described give your applications a professional fit and finish.

As an example of interface programming, Listing 5.6 demonstrates how we can switch back and forth between the two versions of the class that returns file version information.

Listing 5.6 Interface Programming Using the FormAbout Class
 1:  Imports System.Reflection 2: 3:  Public Class FormAbout 4:      Inherits System.Windows.Forms.Form 5: 6:  [ Windows Form Designer generated code ] 7: 8:    Public Shared Sub About() 9:      With New FormAbout() 10:       .ShowDialog() 11:     End With 12: 13:     Dim Form As FormAbout = New FormAbout() 14:     Form.Show() 15: 16: 17:   End Sub 18: 19: 20:   Private Sub FormAbout_Load( _ 21:     ByVal sender As Object, _ 22:     ByVal e As System.EventArgs) Handles MyBase.Load 23: 24:     Dim Info As IVersionInformation = New VersionInformation() 25:     'Dim Info As IVersionInformation = _ 26:       'New ReflectedVersionInformation() 27: 28:     LabelProductName.Text = Info.ProductName 29:     LabelProductVersion.Text = Info.ProductVersion 30:     LabelFileDescription.Text = Info.FileDescription 31:     LabelLegalCopyright.Text = Info.LegalCopyright 32:     LabelComments.Text = Info.Comment 33: 34:   End Sub 35: 36: End Class 

Instead of a specific class, we declare an IVersionInformation variable in line 24, and then we create an instance of a specific class that implements that interface. Comment line 24 and uncomment lines 25 and 26 to switch to the ReflectedVersionInformation type. The balance of the code remains the same because we are requesting data from the interface variable.



Visual Basic. NET Power Coding
Visual Basic(R) .NET Power Coding
ISBN: 0672324075
EAN: 2147483647
Year: 2005
Pages: 215
Authors: Paul Kimmel

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net