【发布时间】:2021-11-03 16:56:06
【问题描述】:
我查看了很多答案,但仍然从以下代码中得到 System.Reflection.TargetException: 'Object does not match target type'。请帮忙
Imports System.Reflection
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tc = New testClass
With tc
.col.Add(10.1)
.col.Add(10.2)
.col.Add(10.3)
Dim col As Collection = getProperty(tc, "col")
txtres.Text = col(2)
End With
End Sub
Public Function getProperty(cls As Object, name As String) As Object
Dim type As Type = cls.GetType()
Dim prop As PropertyInfo = type.GetProperty(name)
Dim col as collection = prop.GetValue(prop.PropertyType, Nothing)
Return col
End Function
Class testClass
Property col As New Collection
End Class
End Class
当我查询Prop时,它的PropertyType返回“collection”,那么不匹配是什么?
?prop
{Microsoft.VisualBasic.Collection col}
Attributes: None {0}
CanRead: True
CanWrite: True
CustomAttributes: Count = 0
DeclaringType: {Name = "testClass" FullName = "Reflection_test.Form1+testClass"}
GetMethod: {Microsoft.VisualBasic.Collection get_col()}
IsSpecialName: False
MemberType: Property {16}
MetadataToken: 385875982
[Module]: {Reflection test.exe}
Name: "col"
PropertyType: {Name = "Collection" FullName = "Microsoft.VisualBasic.Collection"}
ReflectedType: {Name = "testClass" FullName = "Reflection_test.Form1+testClass"}
SetMethod: {Void set_col(Microsoft.VisualBasic.Collection)}
【问题讨论】:
标签: .net vb.net reflection collections