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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/240836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:52:52  来源:igfitidea点击:

Dynamically invoke properties by string name using VB.NET

vb.netreflectioninvoke

提问by Tom

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 feelslike it could be improved. Is there any way to dynamically invoke a property in VB.NET such that something like this can be done:

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

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

采纳答案by Jon Skeet

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 entryabout turning MethodBase.Invokeinto 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.GetSetMethodand then build a delegate which invokes that. You could have a map from field name to "delegate to call to set the field".

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

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 GetPropertymany times, but the step to convert it into a delegate probably isn't worth worrying about.

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

回答by Jonathan Allen

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

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

回答by Jet

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

是的,CallByName 是您的最佳解决方案。这是这样做的说明:

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.

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