Click here to Skip to main content
13,663,621 members
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 7 Dec 2006

A C# alternative for the Visual Basic GetObject function

, 7 Dec 2006
Rate this:
Please Sign up or sign in to vote.
Make the BindToMoniker method work like the Visual Basic GetObject function.


We needed to use an alternative to the Visual Basic GetObject function in C#. Marshal.BindToMoniker would do. But we can not, as in Visual Basic, directly reference the object returned and its methods or properties. We use the obj.GetType().InvokeMember method for this purpose. In this example, we will set the IIS Virtual Directory Basic Authentication using the IIsWebService COM interface.

  1. File -> New -> New Project

    Create a new CSGetObject Visual C# Windows application.

  2. From the main menu, click View -> Tool box.

  3. Drag a Button into the form.

  4. Double click the Button1.

  5. Replace the button1_Click method with the following code:

private void button1_Click(object sender, System.EventArgs e)
  Object obj = Marshal.BindToMoniker("IIS://LocalHost/W3svc/1/Root");

  // Read Property
  bool v = (bool) obj.GetType().InvokeMember("AuthBasic", 
     BindingFlags.DeclaredOnly | 
     BindingFlags.Public | BindingFlags.NonPublic | 
     BindingFlags.Instance | 
     BindingFlags.GetProperty, null, obj, null);
  MessageBox.Show(v.ToString ());

  // Set Property
  // Params: Property or Method, BindingFlags, Binder,
  // Object, array of values or method params
     BindingFlags.DeclaredOnly | 
     BindingFlags.Public | BindingFlags.NonPublic | 
     BindingFlags.Instance | BindingFlags.SetProperty, 
     null, obj, new Object[] {!v});

  // Invoke Method
     BindingFlags.DeclaredOnly | 
     BindingFlags.Public | BindingFlags.NonPublic | 
     BindingFlags.Instance | BindingFlags.InvokeMethod, 
     null, obj, null);
  catch (Exception er)
  MessageBox.Show(er.Message, "Error!!");


using System.Runtime.InteropServices;
using System.Reflection;

to the imports. Build the solution (F7), and run the application (F5).

Points of Interest

That's it!


Just posted.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer CIMEX S.A.
Cuba Cuba
Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[] for any personal message: Ideas on new articles, bibliography about new APIs, questions, are wellcome.

Submit questions related with current article to the article forum.

You may also be interested in...

Comments and Discussions

GeneralThank you!!! Pin
Jamie Nordmeyer14-Jan-09 12:31
memberJamie Nordmeyer14-Jan-09 12:31 
GeneralVery helpful Pin
Yibo Cai25-Jan-07 14:08
memberYibo Cai25-Jan-07 14:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.180810.1 | Last Updated 7 Dec 2006
Article Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid