【发布时间】:2013-10-17 11:15:11
【问题描述】:
我的以下代码已经运行了好几个月,但是我忘记使用Option Strict On 创建这个类,所以现在我要回去正确清理我的代码,但是我无法弄清楚解决以下问题。
我有一个这样声明的局部变量:
Private _manageComplexProperties
现在使用选项 strict,这是不允许的,因为我理解没有 As 子句,但是之所以这样是因为将分配给它的类的实例需要一个类型参数直到运行时才知道。下面的代码解决了这个问题:
Private _type As Type
*SNIP OTHER IRRELEVANT VARIABLES*
Public Sub Show()
Dim requiredType As Type = _
GetType(ManageComplexProperties(Of )).MakeGenericType(_type)
_manageComplexProperties = Activator.CreateInstance(requiredType, _
New Object() {_value, _valueIsList, _parentObject, _unitOfWork})
_result = _manageComplexProperties.ShowDialog(_parentForm)
If _result = DialogResult.OK Then
_resultValue = _manageComplexProperties.GetResult()
End If
End Sub
再次选项 strict 由于后期绑定会引发一些错误,但是一旦我可以成功地正确声明 _manageComplexProperties 变量,就应该使用强制类型转换来清除它们,但我似乎无法获得有效的解决方案,因为直到运行时才知道类型参数。任何帮助将不胜感激。
【问题讨论】:
标签: vb.net type-parameter option-strict