使用 VB.NET 按字符串名称动态调用属性

发布于 2024-07-07 14:40:46 字数 502 浏览 8 评论 0原文

我目前正在开发一个项目,其中一部分代码如下所示:

Select Case oReader.Name
    Case "NameExample1"
        Me.Elements.NameExample1.Value = oReader.ReadString
        ' ...
    Case "NameExampleN"
        Me.Elements.NameExampleN.Value = oReader.ReadString
        ' ...
End Select

它会持续一段时间。 代码显然很冗长,感觉还可以改进。 有没有办法动态调用 VB.NET 中的属性,以便可以完成类似的操作:

Dim sReadString As String = oReader.ReadString
Me.Elements.InvokeProperty(sReadString).Value = sReadString

I'm currently working on a project where a section of the code looks like this:

Select Case oReader.Name
    Case "NameExample1"
        Me.Elements.NameExample1.Value = oReader.ReadString
        ' ...
    Case "NameExampleN"
        Me.Elements.NameExampleN.Value = oReader.ReadString
        ' ...
End Select

It continues on for a while. The code is obviously verbose and it feels like it could be improved. Is there any way to dynamically invoke a property in VB.NET such that something like this can be done:

Dim sReadString As String = oReader.ReadString
Me.Elements.InvokeProperty(sReadString).Value = sReadString

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

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

发布评论

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

评论(3

虫児飞 2024-07-14 14:40:46

我不敢相信其他海报告诉你使用反射。 VB 作为 CallByName 功能完全符合您的要求。

I can't believe the other posters told you to use reflection. VB as a CallByName function that does exactly what you want.

对岸观火 2024-07-14 14:40:46

其他人的回答非常合理,但以防万一这是一段对性能敏感的代码,您可能需要将反射调用编译为委托。

我有一个博客条目 关于将 MethodBase.Invoke 转换为委托。 代码是用 C# 编写的,但同样的技术也可以应用于 VB.NET。 要将其与属性一起使用,请使用 PropertyInfo.GetSetMethod,然后构建一个调用它的委托。 您可以有一个从字段名称到“委托调用以设置字段”的映射。

重申一下,只有在性能关键的代码段中,这才是真正必要的。 否则,您可能仍然希望创建一个 Dictionary 以避免多次调用 GetProperty,但将其转换为委托的步骤可能不值得担心。

Others have answered perfectly reasonably, but just in case this is a performance-sensitive piece of code, you might want to compile the reflective calls into delegates.

I've got a blog entry about turning MethodBase.Invoke into delegates. The code is in C#, but the same technique can be applied to VB.NET as well. To use this with properties, get the appropriate "setter" method with PropertyInfo.GetSetMethod and then build a delegate which invokes that. You could have a map from field name to "delegate to call to set the field".

Just to reiterate, this is only really necessary if it's in a performance-critical piece of code. Otherwise, you might still want to create a Dictionary<string, PropertyInfo> to avoid calling GetProperty many times, but the step to convert it into a delegate probably isn't worth worrying about.

岁月打碎记忆 2024-07-14 14:40:46

是的,CallByName 是您的最佳解决方案。 以下是执行此操作的说明:

CallByName(yourClassOrObjectName,"NameExample1",CallType.Set,oReader.ReadString)

您可以编写“NameExample”来代替“NameExample1”。
请注意,第三个参数允许您“获取”、“让”该参数(甚至调用任何方法)。
因此,您可以使用 CallType.Get 获取参数的值。

Yes, CallByName is the best solution for you. Here's instruction of doing it:

CallByName(yourClassOrObjectName,"NameExample1",CallType.Set,oReader.ReadString)

You can write "NameExample" in place of "NameExample1".
Mention, that third parameter lets you 'Get', 'Let' that parameter (and even invoke any method).
So you can get your parameter's value using CallType.Get.

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