Results 1 to 4 of 4

Thread: InvokeMember and GetMembers

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,369

    InvokeMember and GetMembers

    Following my question about reflection in programming I wrote this code:

    Code:
    Imports System
    Imports System.Reflection
    
    Public Module CoreModule
       Public Sub Main()
          Dim TypeO As Type = Type.GetTypeFromProgID("WScript.Shell")
    
          Console.WriteLine(TypeO.InvokeMember("currentdirectory", BindingFlags.GetProperty, Nothing, Activator.CreateInstance(TypeO), Nothing))
          Console.WriteLine(TypeO.InvokeMember("popup", BindingFlags.InvokeMethod, Nothing, Activator.CreateInstance(TypeO), {"Hello!"}))
          Console.WriteLine(TypeO.InvokeMember("run", BindingFlags.InvokeMethod, Nothing, Activator.CreateInstance(TypeO), {"cmd.exe"}))
    
          For Each MemberO As MemberInfo In TypeO.GetMembers()
             Console.WriteLine(MemberO.Name)
          Next MemberO
       End Sub
    End Module
    All the InvokeMember calls work exactly as expected. However, why can I invoke members exposed by the assembly contained in my TypeO variable but can't list them by probing this same variable using the GetMembers() method? Is GetMembers dealing with another kind of "member" than InvokeMember?

    Also, when I load the very same WScript object into vb6 and look at it using the Object Browser I can tell for a fact those members (run, currentdirectory, popup) are present in its "Shell" class.

    What is going on here? What am I missing?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: InvokeMember and GetMembers

    Without having done any testing, my guess would be that you need to provide the appropriate BindingFlags values when calling GetMembers. You're not providing anything when calling InvokeMember so I'm not sure why the difference but I'd experiment with and see what you can see. Maybe start by specifying Instance and maybe Public too.

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,094

    Re: InvokeMember and GetMembers

    Quote Originally Posted by Peter Swinkels View Post
    What is going on here? What am I missing?
    Try passing some binding flags to GetMembers like in

    GetMembers(BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.InvokeMethod)

    Edit: Oops, this is basically what jmcilhinney proposes too.

    cheers,
    </wqw>

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,369

    Re: InvokeMember and GetMembers

    Okay, I tried a few different binding flags. No methods get returned. I then tried the GetMember method to explicitly search for members with specific name. No results. However, after trying a wildcard (GetMember("*")) this appears and keeps reappearing even after switching back to GetMembers which at first returned nothing at all:

    Code:
    ToString
    GetLifetimeService
    InitializeLifetimeService
    CreateObjRef
    Equals
    GetHashCode
    GetType
    The above looks like something you'd expect from some kind of wrapper. When checking the type for the TypeO variable I get:

    Code:
    Console.WriteLine(TypeO)
    Console.WriteLine(TypeO.BaseType)
    Console.WriteLine(TypeO.GetType)
    Result:
    Code:
    System.__ComObject
    System.MarshalByRefObject
    System.RuntimeType
    The Assembly property for TypeO returns:
    "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width