none
How to determine the number of arguments in order to Invoke a property get method RRS feed

  • Question

  • I have code that uses the Type.InvokeMember. I am spinning thru a MemberInfo[] array and checking each MemberInfo MemberType looking for MemberTypes.Property.  When I find one of type Property, I call Invoke(obj, member.Name, null).

    But, some properties are indexed or have input parameters. Invoking those causes an exception. In the debugger I can examine MemberInfo and I see a "Signature", which I can see tells me how many params, if any, are needed. But, I can't get to that data programmatically. I tried Type.GetMethod(member.Name). But that always returns null. Again, the debugger shows me the GetMethod is "get_xxx" where xxx is the member.Name. So I figured that's what I needed to send to GetMethod. But, again, I cannot get to that name either so I can try to use Type.GetMethod("get_xxx"). I can also see in the debugger watch window a "parameters" array. It is empty for normal properties but is filled with the argument names when a property get API has arguments.

    I hate doing try/catch and invoking every prop. I simply want to find out how many parameters there are to invoke and get the properties with no parameters. The VS debugger's "Dynamic View" is essentially what I want to use in code to auto-test object properties. So, how do I determine how many parameters a Property needs in order to Invoke the property? And, I'm sure I'll hit this too - How do I know I'm invoking the get versus set property for props that are read and write? The debugger watch window shows me the GetMethod and the SetMethod, which is normally "null" since it too is not accessible via code.

    That is, I would like to verify via code the GetMethod exists and has no parameters and then safely Invoke the property

    For background, I am writing an ATP for our COM app that will test every object's APIs that are marked "prop get" in our typelib using reflection.


    R.D. Holland

    Monday, July 8, 2019 7:52 PM

Answers

  • I think that this should work:

    PropertyInfo[] properties = type.GetProperties( );

    foreach( PropertyInfo property in properties )

    {

        MethodInfo getter = property.GetGetMethod( );

        if( getter.GetParameters( ).Length == 0 )

        {

            // regular property with no parameters

            // . . .

        }

    }

     

    But, if you already have a MemberInfo of the property, then it can be casted to PropertyInfo.

    • Marked as answer by RD Holland Tuesday, July 9, 2019 6:51 PM
    Tuesday, July 9, 2019 5:40 AM

All replies

  • I did just do one test. I added "get_" to member.Name and called GetMethod using that string and an empty Type array. The call did return a MethodInfo and the GetParameters call, which is probably not necessary, was empty. But, that seems like cheating.

    R.D. Holland

    Monday, July 8, 2019 8:07 PM
  • I think that this should work:

    PropertyInfo[] properties = type.GetProperties( );

    foreach( PropertyInfo property in properties )

    {

        MethodInfo getter = property.GetGetMethod( );

        if( getter.GetParameters( ).Length == 0 )

        {

            // regular property with no parameters

            // . . .

        }

    }

     

    But, if you already have a MemberInfo of the property, then it can be casted to PropertyInfo.

    • Marked as answer by RD Holland Tuesday, July 9, 2019 6:51 PM
    Tuesday, July 9, 2019 5:40 AM
  • Hi Vorel_

    That works. I owe you big-time.

    Can you help out a bit? I don't know how I could have found that solution. When I use the object browser to go to MemberInfo, it showed it was derived from ICustomAttributeProvider and _MemberInfo. And now when I browse to PropertyInfo, the object browser showed it is derived from MemberInfo and _PropertyInfo.

    I spent a lot of time in the browser trying to determine just what I could do with the MemberInfo object. But, I missed whatever it is that should have clued me in to PropertyInfo, even after following all the derivations and interfaces listed.

    So how did you know you can cast the MemberInfo to a PropertyInfo? Does Type.GetMembers() actually return a PropertyInfo object? The debugger (and signature) simply showed me I had MemberInfo objects.


    R.D. Holland

    Tuesday, July 9, 2019 6:51 PM
  • GetMembers returns a collection of different types derived from abstract MemberInfo. When a breakpoint is reached and a MemberInfo object corresponds to a property, the “Type” column of QuickWatch window, etc., shows that the actual type is RuntimePropertyInfo. This suggests that it is a PropertyInfo (PropertyInfo is an abstract class, RuntimePropertyInfo is the implementation).

    This is probably valid in case of your types too.

    Tuesday, July 9, 2019 7:51 PM
  • I did look at the Type in quickwatch as it is quite useful. But in my case it showed:

    System.Reflection.MemberInfo {Sytem.Reflection.RuntimeMethodInfo}

    Browser failed to find anything when I searched for RuntimeMethodInfo. So I was only left with MemberInfo.

    I have learned one thing. If I accidentally click "Derived Types" in the browser, I can take a long break because it takes a very long time to open the entry!


    R.D. Holland

    Tuesday, July 9, 2019 8:03 PM