结构上的反射集值 [英] Reflection Setvalue on Structure

查看:71
本文介绍了结构上的反射集值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究在结构上使用反射方法。我可以使用GetValue了。问题在于尝试使用SetValue。



我正在测试它的数据结构是实用程序类的成员。以下是结构定义:



 ' 数据结构 
结构 RecordType
Dim 名称作为 字符串
Dim 说明作为 字符串
Dim 输入作为 字符串
Dim 作为 字符串
结束 结构





我正在测试的方法是(位于公共模块中) ):



 公共  Sub  DelimStrToStruct( ByVal  iArr  As  字符串 ByRef  iStruct 作为 对象 ByVal  iDelimChar 作为 字符

Dim tLine() As 字符串 = iArr.Split(iDelimChar)
Dim i 正如 = 0
Dim 字段 As FieldInfo()= iStruct。[ GetType ]()。GetFields(BindingFlags) .Instance BindingFlags。[< span class =code-keyword> Public ])

对于 每个字段作为 FieldInfo 字段中

尝试

field.SetValue(iStruct,tLine(i))

Catch ex 作为 NullReferenceException
'
结束 尝试

i + = 1

下一页字段
结束 Sub





此方法旨在通过分隔字符串,将其拆分为单个元素,然后填充传递的insta具有这些值的结构的nce。



位于上述实用程序类中的字符串调用是:



  Dim  trec  As  RecordType 
DelimStrToStruct (jElem,trec, ^







jElem是一个字符串,其字段由^字符分隔。它在别处定义。



问题是SetValue方法不会改变结构中字段的值。



如何让它正常工作? (仅供参考 - System.Reflection被导入模块)

解决方案

我能够自己开发解决这个问题的方法。它可能不是最优雅的解决方案,但它功能齐全。



我创建了一个新函数来返回结构(作为对象):



 公共 功能 StructSetValue( ByRef  iStruct 作为 对象,< span class =code-keyword> ByVal  iFldName 作为 字符串 ByVal  iValue 作为 对象作为 对象 
Dim tStruct As ValueType = iStruct
Dim field As FieldInfo = tStruct。 [ GetType ]()。GetField(iFldName)
尝试
field.SetValue(tStruct,iValue)
< span class =code-keyword>返回 tStruct
Catch ex 作为 NotSupportedException
返回
结束 尝试
结束 功能





我重写了原始方法作为调用上述函数的函数:



 公共 功能 DelimStrToStruct( ByVal  iArr 作为 字符串 ByRef  iStruct 作为 对象 ByVal  iDelimChar 正如 字符作为 对象 
Dim tLine()作为 String = iArr.Split(iDelimChar)
Dim i As = 0
Dim tStruct As ValueType = iStruct
Dim fields As FieldInfo ()= tStruct。[ GetType ]()。GetFields(BindingFlags.Instance BindingFlags。[公共])
对于 每个字段作为 FieldInfo 字段中
tStruct = StructSetValue(tStruct,field.Name,tLine(i))
i + = 1
下一页字段
返回 tStruct
结束 功能







然后对此的调用变为:



  Dim  trec  As  RecordType 
trec = DelimStrToStruct(jElem,trec, ^





所有这些都可以完成拆箱结构所需的工作。 />


如果有人有任何评论或建议改进,我会更欢迎他们。


I am investigating using reflection methods on structures. I am able to use GetValue ok. The problem is with trying to SetValue.

The data structure I am testing this on is a member of a utility class. Here is the structure definition:

' data structure
Structure RecordType
    Dim Name As String
    Dim Description As String
    Dim Type As String
    Dim Value As String
End Structure



The method I am testing is(Located in a public module):

Public Sub DelimStrToStruct(ByVal iArr As String, ByRef iStruct As Object, ByVal iDelimChar As Char)

    Dim tLine() As String = iArr.Split(iDelimChar)
    Dim i As Short = 0
    Dim fields As FieldInfo() = iStruct.[GetType]().GetFields(BindingFlags.Instance Or BindingFlags.[Public])

    For Each field As FieldInfo In fields

        Try

            field.SetValue(iStruct, tLine(i))

        Catch ex As NullReferenceException
            '
        End Try

        i += 1

    Next field
End Sub



This method is intended to take a passed delimited string, split it into individual elements, and then populate the passed instance of the structure with these values.

The call to the string, located in the above mentioned utility class is:

Dim trec As RecordType
DelimStrToStruct(jElem, trec, "^")




jElem is a string with fields delimited by the "^" character. It is defined elsewhere.

The problem is the SetValue method does not change the value of the fields in the structure.

How do I get this to work correctly? (FYI - "System.Reflection" is imported into the module)

解决方案

I was able to develop the solution to this problem myself. It may not be the most elegant solution, but it is fully functional.

I created a new function to return a structure (as an object):

Public Function StructSetValue(ByRef iStruct As Object, ByVal iFldName As String, ByVal iValue As Object) As Object
    Dim tStruct As ValueType = iStruct
    Dim field As FieldInfo = tStruct.[GetType]().GetField(iFldName)
    Try
        field.SetValue(tStruct, iValue)
        Return tStruct
    Catch ex As NotSupportedException
        Return Nothing
    End Try
End Function



I rewrote the original method as a function that calls the above:

Public Function DelimStrToStruct(ByVal iArr As String, ByRef iStruct As Object, ByVal iDelimChar As Char) As Object
    Dim tLine() As String = iArr.Split(iDelimChar)
    Dim i As Short = 0
    Dim tStruct As ValueType = iStruct
    Dim fields As FieldInfo() = tStruct.[GetType]().GetFields(BindingFlags.Instance Or BindingFlags.[Public])
    For Each field As FieldInfo In fields
        tStruct = StructSetValue(tStruct, field.Name, tLine(i))
        i += 1
    Next field
    Return tStruct
End Function




Then the call to this becomes:

Dim trec As RecordType
trec = DelimStrToStruct(jElem, trec, "^")



All of this does the necessary work to "unbox" the structure.

If anyone has any commentary or suggested improvements, I would more that welcome them.


这篇关于结构上的反射集值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆