2013-02-03 24 views
0

我试图从一个类获取基于字符串的成员,然后获取类型,但我没有任何运气。C#或VB:从属性获取变量类型

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() 

不管我什么组合,我要么什么也得不到或RuntimePropertyType

Dim x = fieldMemberInfo.GetType 

回答

2

如果我正确理解你的问题,你只需要获得“EatsPeople”属性的类型。为此,使用PropertyInfo更容易。

例如

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. 
0

,您就能获得属性名称是这样的:

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