Dynamically invoke properties by string name using VB.NET

31,004

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
31,004

Related videos on Youtube

bmavity
Author by

bmavity

Updated on October 21, 2021

Comments

  • bmavity
    bmavity over 2 years

    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
    
  • perimosocordiae
    perimosocordiae over 14 years
    CallByName looks simpler. How does this differ from Reflection in terms of performance?
  • Jon Skeet
    Jon Skeet over 14 years
    I believe CallByName uses Reflection internally, and I suspect applies various VB-specific shenanigans for backward compatibility. Test it, but I very much doubt that you'll see it being as fast as a delegate. Yes, using MakeDelegate is more complicated, but vastly more performant than reflection.
  • David Rutten
    David Rutten over 12 years
    Holy crap, I wish I knew about this years ago.
  • mrnakumar
    mrnakumar over 8 years
    your blog entry link is broken
  • DavidScherer
    DavidScherer about 5 years
    This is a great quick and dirty method for simple/small use cases. I wouldn't use this for anything complex or where you have lots of 'work' unless performance is not a concern.
  • Jonathan Allen
    Jonathan Allen about 5 years
    If performance is a concern, you should use runtime code generation instead of reflection.