【问题标题】:C# or VB: Get the variable type from a propertyC# 或 VB:从属性中获取变量类型
【发布时间】:2013-01-18 03:27:03
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net reflection types


    【解决方案1】:

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

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

    【讨论】:

      【解决方案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.
      

      【讨论】:

        猜你喜欢
        • 2020-01-29
        • 2021-12-04
        • 2019-01-11
        • 1970-01-01
        • 1970-01-01
        • 2012-07-13
        • 1970-01-01
        • 2014-02-07
        • 2016-10-07
        相关资源
        最近更新 更多