C#或VB:从属性中获取变量类型

时间:2013-02-03 05:01:47

标签: vb.net reflection types

我正在尝试根据类中的String获取成员然后获取类型,但我没有运气。

Public Class Monster
    Public Property Name As String
    Public Property EatsPeople As Boolean
    Public Property Description As String
End Class

要获取会员“EatsPeople”的详细信息,我会这样做:

Dim t = GetType(Monster) ' Get the type of the Product entity.
Dim fieldMemberInfo = t.GetMember("EatsPeople", BindingFlags.IgnoreCase Or BindingFlags.Public Or BindingFlags.Instance).Single()

无论我尝试什么组合,我要么得到Nothing,要么得到RuntimePropertyType

Dim x = fieldMemberInfo.GetType

2 个答案:

答案 0 :(得分:2)

如果我正确理解你的问题,你只想获得" EatsPeople"属性。为此,使用PropertyInfo更容易。

e.g。

Dim t = GetType(Monster) ' Get the type of the Product entity.
Dim propertyInfo = t.GetProperty("EatsPeople") 'Get the property info

Dim x = propertyInfo.PropertyType 'Get the type of the "Eats People" property.

答案 1 :(得分:0)

您可以像这样获取属性名称:

Dim t1 As PropertyInfo = fieldMemberInfo
Dim t2 = t1.PropertyType.FullName