使用指定格式的string.parse()到自定义对象

发布于 2025-01-18 06:07:47 字数 2602 浏览 0 评论 0原文

编辑以重新设计并(希望)简化问题

当我遇到一个问题时,我正在研究一种获取 String 值并将其拆分为其组成部分以填充自定义结构化对象的方法:方法存在用于将字符串解析为“众所周知”的对象,例如 System.Net.IPAddress 或 System.DateTime(例如, DateTime.Parse()DateTime.TryParse()DateTime.ParseExact()),但这些对象使用得很好- 定义的结构不会改变。

没有任何东西(至少,我已经找到了)可以“快速”将字符串解析为自定义的对象/结构。有没有办法通过基本上“反转”基本 String.Format() 方法或字符串插值的功能来提取目标对象的属性来实现相同的结果/给定字符串中的字段值?

为了这个问题和我的示例的目的,我将这个假设的方法称为 String.Parse()

例如,给定一个具有以下定义的对象:

Public Class LastModified
    Public Property User As String = Nothing
    Public Property FromIP As String = Nothing
    Public Property InterfaceName As String = Nothing
    Public Property Method As String = Nothing
    Public Property ModifiedDate As DateTime = DateTime.MinValue
End Class

我正在考虑类似以下伪代码替代方案之一:

' PSEUDOCODE - NOT OPERATIONAL - DO NOT USE
Dim LMBString As String = "admin 192.168.168.168:X0 UI 03/26/2022 15:43:38"
Dim LMB As New LastModified

' USING "BASIC" String.Format() SYNTAX
LMBString.Parse("{0} {1}:{2} {3} {4}", 
                LMB.User,
                LMB.FromIP,
                LMB.InterfaceName,
                LMB.Method,
                LMB.ModifiedDate)

' USING STRING INTERPOLATION SYNTAX
LMBString.Parse($"{LMB.User} {LMB.FromIP}:{LMB.InterfaceName} {LMB.Method} {LMB.ModifiedDate}")

也许此功能可以通过第 3 方库获得,我想我可能可以构建一个 .Parse()可以实现这一目标的扩展方法相对容易,但我想继续看看其他人是否已经发明了那个特殊的轮子,而我的Google-fu可能太弱了。如上所述,我无法找到当前在 .NET 中本机可用的这些伪代码操作的实现(我在当前项目中使用 Framework 4.7),但我不知道介意问一个可能有简单解决方案的问题看起来很愚蠢——特别是如果它可以帮助面临同样问题的其他人。


仅供参考,在我当前的实现中,我使用以下(简化的)代码,该代码功能齐全并提供了我正在寻找的结果。我很满意继续使用这个“原样”,但我忍不住想知道是否有一种方法可以用更少的代码行来实现相同的目标。

Public Function ParseStringToLastModifiedObject As LastModified
    Dim LMBString As String = "admin 192.168.168.168:X0 UI 03/26/2022 15:43:38"
    Dim LMBBuffer As String() = LMBString.Split(" "c)
    Dim LMBReturn As New LastModified

    With LMBReturn
        Dim IPBuffer As String() = LMBBuffer(1).Split(":"c)

        .User = LMBBuffer(0) '-> "admin"
        .Method = LMBBuffer(2) '-> "UI"
        .ModifiedDate = CDate(LMBBuffer(3) & " " & LMBBuffer(4)) '-> #3/26/2022 15:43:38#
        .FromIP = IPBuffer(0) '-> "192.168.168.168"
        .InterfaceName = IPBuffer(1) '-> "X0"
    End With

    Return LMBReturn
End Function

EDITED TO REWORK AND (HOPEFULLY) SIMPLIFY THE QUESTION

I was working on a method for taking a String value and splitting it into its component parts for populating a custom structured object when a question occurred to me: Methods exist for parsing strings into "well-known" objects like System.Net.IPAddress or System.DateTime (e.g., DateTime.Parse(), DateTime.TryParse(), or DateTime.ParseExact()), but these objects use well-defined structures that don't change.

Nothing (at least, that I've been able to find) is available that can "quickly" parse a string into a custom-defined object/structure. Is there a way to achieve the same result by basically "reversing" the functionality of either the base String.Format() method or string interpolation to extract a target object's property/field values from a given string?

For the purposes of this question and my examples, I'll refer to this hypothetical method as String.Parse()

For example, given an object with the following definition:

Public Class LastModified
    Public Property User As String = Nothing
    Public Property FromIP As String = Nothing
    Public Property InterfaceName As String = Nothing
    Public Property Method As String = Nothing
    Public Property ModifiedDate As DateTime = DateTime.MinValue
End Class

I was thinking of something like one of the following pseudocode alternatives:

' PSEUDOCODE - NOT OPERATIONAL - DO NOT USE
Dim LMBString As String = "admin 192.168.168.168:X0 UI 03/26/2022 15:43:38"
Dim LMB As New LastModified

' USING "BASIC" String.Format() SYNTAX
LMBString.Parse("{0} {1}:{2} {3} {4}", 
                LMB.User,
                LMB.FromIP,
                LMB.InterfaceName,
                LMB.Method,
                LMB.ModifiedDate)

' USING STRING INTERPOLATION SYNTAX
LMBString.Parse(
quot;{LMB.User} {LMB.FromIP}:{LMB.InterfaceName} {LMB.Method} {LMB.ModifiedDate}")

Perhaps this functionality is available through a 3rd-party library, and I suppose I could probably build a .Parse() extension method that could achieve this goal relatively easily, but I wanted to go ahead and see if someone else had already invented that particular wheel, and my Google-fu may simply be too weak. As stated above, I've not been able to find implementations of either of these pseudocode operations currently available natively in .NET (I'm using Framework 4.7 for my current project), but I don't mind looking stupid to ask a question that might have a simple solution - especially if it helps someone else who faces the same issues.


Just for reference purposes, in my current implementation, I'm using the following (simplified) code, which is totally functional and provides the results I'm looking for. I'm satisfied to keep using this "as-is", but I couldn't help wondering if there might be a way to achieve the same goal with fewer lines of code.

Public Function ParseStringToLastModifiedObject As LastModified
    Dim LMBString As String = "admin 192.168.168.168:X0 UI 03/26/2022 15:43:38"
    Dim LMBBuffer As String() = LMBString.Split(" "c)
    Dim LMBReturn As New LastModified

    With LMBReturn
        Dim IPBuffer As String() = LMBBuffer(1).Split(":"c)

        .User = LMBBuffer(0) '-> "admin"
        .Method = LMBBuffer(2) '-> "UI"
        .ModifiedDate = CDate(LMBBuffer(3) & " " & LMBBuffer(4)) '-> #3/26/2022 15:43:38#
        .FromIP = IPBuffer(0) '-> "192.168.168.168"
        .InterfaceName = IPBuffer(1) '-> "X0"
    End With

    Return LMBReturn
End Function

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文