[VB.NET]Attribute與反射的搭配使用
Property Data as Object
End Class
Property RelativedType As Type
Sub New(ByVal relativedType As Type)
Me.RelativedType = relativedType
End Sub
End Class
Enum ApplicationType <RelativedType(GetType(LevelUp.Office.Word))> _ Word
<RelativedType(GetType(LevelUp.Office.Excel))> _
Excel
<RelativedType(GetType(LevelUp.Office.Access))> _
Access
<RelativedType(GetType(LevelUp.Office.PowerPoint))> _
PowerPoint
<RelativedType(GetType(LevelUp.Office.Visio))> _
Visio
End Enum
Sub StartApplication(ByVal apType As ApplicationType)
Dim type As Type = apType.GetType()
Dim field = type.GetField(apType.ToString)
Dim att = field.GetCustomAttributes(GetType(RelativedTypeAttribute), False).Cast(Of RelativedTypeAttribute)().FirstOrDefault()
Activator.CreateInstance(att.RelativedType).Start()
End Sub</pre></div>
Property ResourceID As String
Sub New(ByVal resourceID As String)
Me.ResourceID = resourceID
End Sub
End Class
Enum InterfaceType <ResouceID(“RS232_InterfaceName”)> _ RS232
<ResouceID("RS485_InterfaceName")> _
RS485
<ResouceID("GPIB_InterfaceName")> _
GPIB
<ResouceID("I2C_InterfaceName")> _
I2C
End Enum