Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Using Reflection to get the name of a function parameter.

Using Reflection to get the name of a function parameter.

Scheduled Pinned Locked Moved .NET (Core and Framework)
questiondatabasedocker
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    howardjr
    wrote on last edited by
    #1

    I wrote a routine using some of the reflection features to look for paired member fields in a class. The function looks up this variable's value and the value of the paired variable, which may or may not actually exist. If it does, then its boolean value is read, and if true the function returns a System.DBNull.value, otherwise the function just returns the value of the named field passed into it when called. Rather than use variant variables to store my database fields, I'm using two fields to store the value read from a database. The first is a typed field variable for the actual data, if it's not Null, and the second is a boolean to track if the database value was Null or not, i.e., Name and Name_Is_Null, both of which belong to an instance of the DataBase_Info class. Currently, the function is called with a string containing the name of one of the member fields, and there is only one instance of DataBase_Info. Now I'd like to pass the actual field variable to the function, and have resolve the class instance, if there is a paired member, and return either the value passed in if their isn't a paired member or it's false, otherwise return System.DBNull.value. How can I resolve the name of a variable passed in as a parameter and its container at run-time? Thank you

    H 1 Reply Last reply
    0
    • H howardjr

      I wrote a routine using some of the reflection features to look for paired member fields in a class. The function looks up this variable's value and the value of the paired variable, which may or may not actually exist. If it does, then its boolean value is read, and if true the function returns a System.DBNull.value, otherwise the function just returns the value of the named field passed into it when called. Rather than use variant variables to store my database fields, I'm using two fields to store the value read from a database. The first is a typed field variable for the actual data, if it's not Null, and the second is a boolean to track if the database value was Null or not, i.e., Name and Name_Is_Null, both of which belong to an instance of the DataBase_Info class. Currently, the function is called with a string containing the name of one of the member fields, and there is only one instance of DataBase_Info. Now I'd like to pass the actual field variable to the function, and have resolve the class instance, if there is a paired member, and return either the value passed in if their isn't a paired member or it's false, otherwise return System.DBNull.value. How can I resolve the name of a variable passed in as a parameter and its container at run-time? Thank you

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Rather than writing your own mechanism, why not just use strongly typed data sets, which do this all for you? They're easy to create, very robust, and with .NET 2.0 very flexible to change the underlying connection and fetch behaviors. Take a look at many of the search results on MSDN from http://search.msdn.microsoft.com/search/default.aspx?siteId=0&tab=0&query=strongly+typed+dataset[^]. As far as answering your question, you can use Type.Method or Type.GetMethods. With each MethodInfo, you can call MethodInfo.GetParameters. There are also other interesting and pertinent properties on MemberInfo you may need to pay attention to, like IsGenericMethod. Just depends on what methods you're reflecting and how they're written.

      This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

      H 1 Reply Last reply
      0
      • H Heath Stewart

        Rather than writing your own mechanism, why not just use strongly typed data sets, which do this all for you? They're easy to create, very robust, and with .NET 2.0 very flexible to change the underlying connection and fetch behaviors. Take a look at many of the search results on MSDN from http://search.msdn.microsoft.com/search/default.aspx?siteId=0&tab=0&query=strongly+typed+dataset[^]. As far as answering your question, you can use Type.Method or Type.GetMethods. With each MethodInfo, you can call MethodInfo.GetParameters. There are also other interesting and pertinent properties on MemberInfo you may need to pay attention to, like IsGenericMethod. Just depends on what methods you're reflecting and how they're written.

        This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

        H Offline
        H Offline
        howardjr
        wrote on last edited by
        #3

        Thank you for your reply. I've tried to stay away from using strongly typed data sets in favor of "self-healing" code. I'm forced to use VS2003 with .Net 1.1, so this may not be a problem with the later version(s), but when the database changes, i.e., a column goes from not null to null, we've had to update the strongly typed data sets and the programs. I'm more interested in having the code able to deal with nulls directly. However, I'll need to look at the link you gave, it may be that we're not using stongly typed data sets properly. As I said in my first posting, I want to be able to create a class structure instance to contain all of my database column data. Currently I'm testing the columns for null as they are read from the database using a query. If the value is null then I set a paired member flag variable to true and a 0 or empty string, or some other default value in the value member variable, and if the column isn't null, then I'm setting the flag to false and store the column value in another member variable. The flag member adds the suffix '_Is_Null' to the name of the one used by the value member. This works well enough, I just need to check the '_Is_Null' suffixed flag member to see if the database value was Null or not to determine how to use the value member variable. However, when it came to sending values back to the database via a stored procedure I wanted to "hide" this code so the assignments easier to read. Instead of passing both the flag member variable and the value member variable to the function in which I was hiding the test, I decided to pass the name of the value member into the function, and use Reflections to look up the paired flag member and its value, then return the appropriate value to be assigned to the stored procedure's parameter. This too works well enough, except that because I was passing in the name of the member as a string instead of the member's value, I'm not able to take advantage of the compiler check for misspelled member names. So, I want to use the value member instead of its name in the function call. However, when I tried using them with the debugger I could see the data types of the parameters, but I couldn't figure out how to get the names of the variables passed in to them with the function call. Another issue is that Reflections tells me what the containing class's type name is, but not the instanced variable, so if I had more than one instance of the class, how would I know that I'm looking at the right instance when I resolve the

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups