元属性继承可以使用IsDefined函数进行判断,先写出结论
如果使用Overrides,则元属性可以继承,除非在使用IsDefined时明确不进行继承判断,如
pFunction.IsDefined(GetType(DisplayNameAttribute), False)
如果使用Overloads,则元属性不能继承,
如下为测试源码:
Sub Main()
‘Dim s As New S3()
‘s.Method()
‘CType(s, S2).Method()
‘CType(s, S1).Method()
Dim pFunction As MethodInfo = GetType(S2).GetMethod("Method")
If (pFunction.IsDefined(GetType(DisplayNameAttribute), False)) Then
Console.WriteLine("DisplayName")
Else
Console.WriteLine("No DisplayName")
End If
Console.ReadLine()
End Sub
End Module
Class S1
<DisplayName("s1")>
Public Overridable Sub Method()
Console.WriteLine("这是S1实例方法")
End Sub
End Class
Class S2
Inherits S1
Public Overrides Sub Method()
Console.WriteLine("这是S2实例方法")
End Sub
End Class
Class S3
Inherits S2
‘Public Overloads Sub Method()
‘ Console.WriteLine("这是S3实例方法")
‘End Sub
End Class
时间: 2024-10-31 15:00:06