vb.net的反射之GetType.GetProperties取到数组为空的原因
最近在进行一个介于PLC与MES之间的通讯程序,从PLC读数据再组XML上报MES.
组XML使用了XStream,为了方便使用定义了一个所有成员都公开的类,偷懒吗 所以...
Public Class PLCInfo Public Eqp As String Public Fab As String Public Area As String Public QName As String Public PlcIp As String Public PlcPort As String Public PlcNetNo As String Public PlcStation As String Public PcNetNo As String Public PcStation As String Public TRX As String Public RefreshTime As String Public P_PLCReady As String Public P_PCRead As String Public P_EqpName As String Public P_Chip As String Public P_Model As String Public isGenChip As String End Class
因为这是个.xml的配置文件 里面有PLC点位,为了可扩展就想用反射取PLC点位的值,myType.GetProperties都是空的...
'准备PLC点位资讯 Dim myType As Type = GetType(BootConf.PLCInfo) For Each t As System.Reflection.PropertyInfo In myType.GetProperties Dim tagName As String = t.Name Dim value As String = t.GetValue(objPlcInfo, Nothing) '... Next
为啥捏?这么简单的应用不应该有问题啊,各种翻各种找,最后在MSDN看到:
---------------------------------------------------------------------
返回值
表示当前 Type 的所有公共属性 (Property) 的 PropertyInfo 对象数组。 - 或 - 如果当前 Type 没有公共属性 (Property),则为 PropertyInfo 类型的空数组。
--------------------------------------------------------------------
原来如此 GetProperties取的对象一定要有Property,要取的对象加个Property,搞定
Public Class PLCInfo Public Eqp As String Public Fab As String Public Area As String Public QName As String Public PlcIp As String Public PlcPort As String Public PlcNetNo As String Public PlcStation As String Public PcNetNo As String Public PcStation As String Public TRX As String Public RefreshTime As String Public P_PLCReady As String Public P_PCRead As String Public Property P_EqpName As String Public Property P_Chip As String Public Property P_Model As String Public isGenChip As String End Class