Alex Guerin
Alex Guerin

Reputation: 2386

C# or VB: Get the variable type from a property

I'm trying get a member based on a String from a class and then get the type but I'm not having any luck.

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

To get the details for the member "EatsPeople" I do:

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

No matter what combo I try, I either get Nothing or a RuntimePropertyType

Dim x = fieldMemberInfo.GetType

Upvotes: 0

Views: 2512

Answers (2)

aaa
aaa

Reputation: 1124

You can get the property Name like this:

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

Upvotes: 0

Nathan
Nathan

Reputation: 10764

If I understand your question correctly, you simply want to get the type of the "EatsPeople" property. For that, it is easier to use 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.

Upvotes: 3

Related Questions