【发布时间】:2016-01-12 22:26:43
【问题描述】:
当我尝试分配 _val 时,我得到“对象与目标类型不匹配。”
我验证了两个 PropertyInfo 都是 system.string。我还找到了这种语法的例子。感谢您的帮助。
Private Function SetAttributesForSplitFiles(ByVal _file As String, ByVal _depHeader As HeaderParse)
Dim _fileMask As New FileMaskExtension()
Dim _type As Type = GetType(HeaderParse.depBackupFileProperties)
For Each _prop As Reflection.PropertyInfo In GetType(OutputMgr.Interface.FileMaskExtension).GetProperties()
Dim _headerProperty As PropertyInfo = _type.GetProperty(_prop.Name)
Dim _val = _headerProperty.GetValue(_depHeader)
_prop.SetValue(_fileMask, _val, Nothing)
Next
SendFileTODepcon(_fileMask, _file)
End Function
【问题讨论】:
-
首先,您还没有定义 _val 的类型,例如将 _val 调暗为整数。在该行设置断点并将鼠标悬停在 _val 上以查看 IDE 认为它是什么类型,然后将鼠标悬停在 GetValue 上以查看它应该是什么类型。然后将 _val 定义为适当的类型,如果需要,将 _headerProperty.GetValue(_depHeader) 转换为适当的类型
-
哦,在 Visual Studio 设置中,ALWAYS 将 Option Strict 设置为 On。它会拾取这些类型的错误。关闭它的原因很少。
-
我添加了 Option Stict ON 但我仍然收到错误。
-
我添加了这行代码
-
Dim _val As Object = _headerProperty.GetValue(_type)
标签: vb.net properties propertyinfo