Obligation to call a method

▼魔方 西西 提交于 2019-12-11 00:25:48

问题


In VB .NET, when you create a user control class, you have the obligation to call the sub InitializeComponent within the constructor.

If you don't you'll a warning message like this :

'Public Sub New()' in designer-generated type 'MyUserControl' should call InitializeComponent method.

What is the mechanism used to raise this warning? Is it something I can reproduce for my own functions?


回答1:


This is built-in behavior for the VB.NET compiler. This sample class triggers it:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Class1
    Public Sub New()
       '' Warning BC40054 generated here
    End Sub
End Class

The attribute matters. It is not otherwise generically useful behavior, you cannot tell the compiler to make it generate a similar warning using your own attribute.



来源:https://stackoverflow.com/questions/4817836/obligation-to-call-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!