将结构转换为字符串 [英] Converting Structure to String

查看:49
本文介绍了将结构转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了这样的公共结构:

I have defined such public structure:

Public Structure myList
    Dim a As String
    Dim b As Integer
    Dim c As Double
End Structure

稍后在代码中我为它的实例赋值:

Later in code I assign values to it's instance:

    Dim myInstance As New myList
    myInstance.a = "Nemo"
    myInstance.b = 10
    myInstance.c = 3.14

然后我可以将这些值转换为字符串(用于存储到数据库),如下所示:

And then I can convert those values to string (for storing to database) like this:

    Dim newString As String = ""
    Dim i As Integer
    Dim myType As Type = GetType(myList)
    Dim myField As System.Reflection.FieldInfo() = myType.GetFields()
    For i = 0 To myField.Length - 1
        newString &= myField(i).GetValue(myInstance) & " "
    Next i

其中字符串newString"包含每个字段的值.一切正常.

Where string "newString" contain values of each field. All of that works OK.

现在我想为程序中的几种不同结构进行这种转换,但我不能(不知道如何)将某些结构和实例传递给函数.

Now I would like to make a function for such converting for several different structures which I have in program but I can't (don't know how) to pass certain structure and instance to function.

我的尝试:

Public Function StructToString(ByVal myStruct As System.Type) As String

    Dim structString As String = ""
    Dim i As Integer
    Dim myType As Type = GetType(myStruct)
    Dim myField As FieldInfo() = myType.GetFields()
    For i = 0 To myField.Length - 1
        newString &= myField(i).GetValue(Nothing) & " " 
    Next i

    Return structString
End Function

但这不起作用,因为结构无法转换为类型.

But that don't work since structure cannot be converted to type.

这里是否有可能制作这样的函数来将各种结构转换为字符串(可以放置到一些公共模块中)以及如何正确地做到这一点?

Is here possibility to make such function for converting various structures to string (which can be placed to some public module) and how to do that properly?

推荐答案

只需将要转换为字符串的实例作为 Object 传递并使用 GetType()获取实例的Type的实例方法.

Just pass the instance you want to transform to a string as Object and use the GetType() instance method to get the Type of the instance.

Public Function StructToString(obj As Object) As String

    Dim structString As String = ""
    Dim i As Integer
    Dim myType As Type = obj.GetType()
    Dim myField As FieldInfo() = myType.GetFields()
    For i = 0 To myField.Length - 1
        structString += myField(i).GetValue(obj) & " " 
    Next i

    Return structString
End Function

请注意,您的字符串将以空格结尾,因此您可能应该使用 String.Join 进行字符串连接.这是您编写的单行函数:

Note that your string will end with a space, so you probably should use String.Join for string concatenation. Here's your function written as a one-liner:

Public Function StructToString(obj As Object) As String
    Return String.Join(" ", obj.GetType().GetFields().Select(Function(field) field.GetValue(obj)))
End Function

请注意,如果任何字符串字段包含空格,您在数据库中存储内容的系统将中断.为什么不创建一个合适的数据库模式或使用序列化?或者至少使用另一个分隔符而不是空格.

Note that your system of storing stuff in a database like this will break if any string field will contain a space. Why not just create a proper database schema or use serialization? Or at least use another seperator than space.

此外,您是否有任何特定理由将结构与公共可变字段一起使用?如果没有,你真的应该使用类和属性而不是结构和字段.

Also, have you any specific reason to use structs with public mutable fields? If not, you should really use classes and properties instead of structs and fields.

这篇关于将结构转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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