从自定义列表转换​​为字符串列表

发布于 2024-08-26 17:09:04 字数 539 浏览 3 评论 0原文

大家好,我有以下代码:

    Public Shared Function ConvertToString(ByVal list As IList) As String
        Dim strBuilder = New System.Text.StringBuilder()
        Dim item As Object
        For Each item In list
            strBuilder.Append(obj.ToString())
            strBuilder.Append(",")
        Next
        Return strBuilder.ToString(strBuilder.Length - 1)
    End Function

目的是将自定义对象的 IList 转换为包含 Ilist 中每个元素的等效字符串。不幸的是,我似乎找不到一种方法来获取自定义对象的基础数据,当然,如上面的示例所示,使用对象只是给了我一串类型定义,而不是访问基础数据。非常感谢任何帮助。

保罗.

Hi all I have the following code:

    Public Shared Function ConvertToString(ByVal list As IList) As String
        Dim strBuilder = New System.Text.StringBuilder()
        Dim item As Object
        For Each item In list
            strBuilder.Append(obj.ToString())
            strBuilder.Append(",")
        Next
        Return strBuilder.ToString(strBuilder.Length - 1)
    End Function

The intention is to convert an IList of custom objects to a string equivalent comprising each element in the Ilist. Unfortunately I can't seem to find a way to get the underlying data of the custom object, and of course as in the above example, using object simply gives me a string of types definitions, rather than access to the underlying data. Any assistance much appreciated.

Paul.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秉烛思 2024-09-02 17:09:04

对象中没有“基础数据”的默认字符串表示形式。这完全取决于您想看到什么。例如,您有一个 Person 类,它具有 FirstName 和 LastName 属性。您有一个实例,其中 FirstName =“John”且 LastName =“Smith”。基础数据的默认表示是什么? “约翰·史密斯”? “史密斯、约翰”?还有别的事吗?

这就是(我认为)如果您没有重写该方法以显示更有用的内容,.NET 在 ToString 方法中返回类型名称的原因。该框架无法知道什么是任何给定类的基础数据的有用表示。

所以我认为你不能让你的方法适用于任意类。如果您希望它适用于一小部分特定的类,您可以按照 Paul Sasik 的建议重写 ToString,为它们提供有用的字符串表示形式。或者,如果您无权访问这些类的代码,则可以为所有类添加扩展方法(GetUnderlyingData 或类似方法),然后调用该扩展方法而不是 ToString。

There's no default string representation of "the underlying data" in an object. It all depends on what you want to see. Say, for example, you have a Person class, and it has properties FirstName and LastName. You have an instance where FirstName = "John" and LastName = "Smith". What would the default representation of the underlying data be? "John Smith"? "Smith, John"? Something else?

That's (I assume) why .NET returns the type name in the ToString method if you haven't overridden that method to display something more useful. The framework has no way of knowing what would be a useful representation of the underlying data of any given class.

So I don't think you can make your method work for arbitrary classes. If you have a specific small set of classes you want this to work for, you can override ToString as Paul Sasik suggests to provide a useful string representation for them. Or, if you don't have access to the code for those classes, you could add an extension method for all of them, GetUnderlyingData or something like that, and call that extension method instead of ToString.

从﹋此江山别 2024-09-02 17:09:04

如果您可以控制自定义对象,则可以重写 ToString 函数以返回您想要查看的字符串数据类型。

I can't seem to find a way to get the underlying data of the custom object,

怎么会?你尝试了什么?您应该能够转换为类型或通过反射获取它。也许您可以向我们展示更多代码?

If you have control over the custom object you could override the ToString function to return the type of string data that you want to see.

I can't seem to find a way to get the underlying data of the custom object,

How come? What have you tried? You should be able to cast to the type or get it by reflection. Maybe you can show us a little more code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文