[英]Use shared/static Property with Activator.CreateInstance
I'm upgrading a .NET 2.0 WinForms application to .NET 4.5.2 and am receiving two warning on this block of code: 我正在将.NET 2.0 WinForms应用程序升级到.NET 4.5.2,并且在此代码块上收到两个警告:
Public Sub ShowFormAsMdiChild(ByVal newFormType As Type, _
ByVal mdiParentType As Type, _
Optional ByVal SearchID As String = "",
Optional ByVal curType As SearchType = SearchType.Residential)
If SearchID = "" Then
Dim F As Form
F = CType(Activator.CreateInstance(newFormType), Form)
F.MdiParent = CType(Activator.CreateInstance(mdiParentType), Form).ActiveForm
F.Show()
Else
Dim F As Form
Dim args(1) As Object
args(0) = SearchID
args(1) = curType
F = CType(Activator.CreateInstance(newFormType, args), Form)
F.MdiParent = CType(Activator.CreateInstance(mdiParentType), Form).ActiveForm
F.Show()
End If
End Sub
I receive the following warning on the two lines that set F.MdiParent: 我在设置F.MdiParent的两行上收到以下警告:
Access of shared member, constant member, enum member or nested type through an instance; 通过实例访问共享成员,常量成员,枚举成员或嵌套类型; qualifying expression will not be evaluated. 合格表达式将不被评估。
The problem is that the ActiveForm Property is a shared Property and because the code is creating a new instance of the Form, calling the a Form's shared Property causes the warning. 问题在于ActiveForm属性是共享属性,并且由于代码正在创建Form的新实例,因此调用Form的共享属性会导致警告。
Is there a better way to implement without the warning? 有没有更好的方法来实施而不发出警告? Preferred answer in VB.NET, but I can translate if you only know how in C#. VB.NET中的首选答案,但是如果您只懂C#,我可以翻译。
There's no need to create a new instance of the MDI parent type, and then cast it to Form
. 无需创建MDI父类型的新实例,然后将其转换为Form
。 ActiveForm
is a shared property of the Form
class, so you should access it through the class name, not through an instance. ActiveForm
是Form
类的共享属性,因此您应该通过类名而不是实例来访问它。
F.MdiParent = Form.ActiveForm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.