B
Bob
If two attribute instances of the same type have all their fields values
identical, the compiler will say that they are not equal to each other, yet
adding them both as keys in a hashtable will throw an exception. Is this
expected behavior or a bug?
I've included an example below. Run the code as is and no exception is
thrown. Run it with both attributes contructed identically, or remove all
fields in the TestAttribute class, and an exception is thrown.
Bob
------------------------
Imports System.ComponentModel
Imports System.Reflection
Module Main
Public Sub main()
Try
Dim ht As New Hashtable
For Each t As Type In [Assembly].GetExecutingAssembly.GetTypes
For Each atr As Attribute In
t.GetCustomAttributes(GetType(TestAttribute), False)
ht.Add(atr, "some value")
Next
Next
MsgBox("no error")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Module
<AttributeUsage(AttributeTargets.Class)> _
Public Class TestAttribute
Inherits Attribute
Public Sub New(ByVal ifoo As String)
Foo = ifoo
End Sub
Public ReadOnly Foo As String
End Class
<TestAttribute("test1")> _
Public Class test1
End Class
<TestAttribute("test2")> _
Public Class test2
End Class
identical, the compiler will say that they are not equal to each other, yet
adding them both as keys in a hashtable will throw an exception. Is this
expected behavior or a bug?
I've included an example below. Run the code as is and no exception is
thrown. Run it with both attributes contructed identically, or remove all
fields in the TestAttribute class, and an exception is thrown.
Bob
------------------------
Imports System.ComponentModel
Imports System.Reflection
Module Main
Public Sub main()
Try
Dim ht As New Hashtable
For Each t As Type In [Assembly].GetExecutingAssembly.GetTypes
For Each atr As Attribute In
t.GetCustomAttributes(GetType(TestAttribute), False)
ht.Add(atr, "some value")
Next
Next
MsgBox("no error")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Module
<AttributeUsage(AttributeTargets.Class)> _
Public Class TestAttribute
Inherits Attribute
Public Sub New(ByVal ifoo As String)
Foo = ifoo
End Sub
Public ReadOnly Foo As String
End Class
<TestAttribute("test1")> _
Public Class test1
End Class
<TestAttribute("test2")> _
Public Class test2
End Class