Overloads和Overrides在元属性继承上的特性

元属性继承可以使用IsDefined函数进行判断,先写出结论
如果使用Overrides,则元属性可以继承,除非在使用IsDefined时明确不进行继承判断,如
pFunction.IsDefined(GetType(DisplayNameAttribute), False)
如果使用Overloads,则元属性不能继承,
如下为测试源码:

  1. Sub Main()
  2. 'Dim s As New S3()
  3. 's.Method()
  4. 'CType(s, S2).Method()
  5. 'CType(s, S1).Method()
  6. Dim pFunction As MethodInfo = GetType(S2).GetMethod("Method")
  7. If (pFunction.IsDefined(GetType(DisplayNameAttribute), False)) Then
  8. Console.WriteLine("DisplayName")
  9. Else
  10. Console.WriteLine("No DisplayName")
  11. End If
  12. Console.ReadLine()
  13. End Sub
  14. End Module
  15. Class S1
  16. <DisplayName("s1")>
  17. Public Overridable Sub Method()
  18. Console.WriteLine("这是S1实例方法")
  19. End Sub
  20. End Class
  21. Class S2
  22. Inherits S1
  23. Public Overrides Sub Method()
  24. Console.WriteLine("这是S2实例方法")
  25. End Sub
  26. End Class
  27. Class S3
  28. Inherits S2
  29. 'Public Overloads Sub Method()
  30. ' Console.WriteLine("这是S3实例方法")
  31. 'End Sub
  32. End Class

原文地址:https://www.cnblogs.com/wene/p/5246165.html