C#: Using reflection with COM objects

Yesterday, I was trying to use reflection to access some properties of a COM object but was getting the following exception:

Object reference not set to an instance of an object

Debugging into, I noticed that GetProperty() method was returning null in the following call:

var propertyInfo = comObject.GetType().GetProperty("PropertyName")

After some searching, I found that we need to use the more generic InvokeMember() method to get or set the properties of a COM object. Here’s the simplest example of the usage:

//get the value of comObject.PropertyName
object propertyValue = comObject.GetType().InvokeMember("PropertyName", System.Reflection.BindingFlags.GetProperty, null, comObject, null);

//set the value of comObject.PropertyName
comObject.GetType().InvokeMember("PropertyName", System.Reflection.BindingFlags.SetProperty, null, comObject, new object[] { propertyValue });

I hope this post helps someone who is trapped into the same situation.

About these ads

16 Responses to “C#: Using reflection with COM objects”

  1. DotNetShoutout Says:

    C#: Using reflection with COM objects « Mehroz’s Experiments…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. Sergio Says:

    NOt work
    propertyValue have comobject and not propertys of it

  3. Syed Mehroz Alam Says:

    I didn’t get you, Sergio. The code works fine and successfully grabs the respective property. Are you sure your syntax is correct. Do note that with reflection, you have to be extra cautious because there are no compile time checking.

  4. David Says:

    Hi Mehroz, this is great but how can I then Set the value for the property?

  5. Syed Mehroz Alam Says:

    Hi David,

    The code under the comment: //set the value of comObject.PropertyName in the post can be used to set the property. Do you have any problems with that?

  6. David Says:

    Im trying to override a private set

    using MSProject = Microsoft.Office.Interop.MSProject;
     MSProject.Resource resource = this.Application.ActiveProject.Resources.Add("PersonName",missing);
    resource.GetType().InvokeMember("UniqueID", BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, resource, new object[] { iUid });
    

    im getting an exception “Exception has been thrown by the target of an invocation.”, {“Number of parameters specified does not match the expected number.”}

    im not to sure how to work out what other paramters it wants?

  7. Syed Mehroz Alam Says:

    David,

    The code seems correct. Are you sure that MSProject.Resource object has a “private” setter for UniqueID (I think it is a “readonly” property with only the get part implemented). You can confirm this using the Object Browser in Visual Studio.

    public class TestClass
    {
        private string privateField;
    
        private string PrivateProperty
        {
            get { return this.privateField; }
            set { this.privateField = value; }
        }
    
        public string PublicPropertyPrivateSet
        {
            get { return this.privateField; }
            private set { this.privateField = value; }
        }    
    
        public string ReadOnlyField
        {
            get { return this.privateField; }
        }
    }
    
  8. David Says:

    int UniqueID { get; }
    Member of Microsoft.Office.Interop.MSProject.Resource

  9. Syed Mehroz Alam Says:

    Yeah. It is a readonly property with no setter and hence can not be written.
    Notice the code snippet in my previous reply. All the members: privateField, PrivateProperty, PublicPropertyPrivateSet can be written using reflection except: ReadOnlyField.

    Hope this answers your question

  10. TG Says:

    THANK YOU!!!!!

  11. mofo22 Says:

    Thank you,

    you save me lot of time :-)

  12. achi Says:

    Hi, i know a year and more is pass, but can you send me an example? i need to understand how the com object is declared. I’m facing a call to a method of a com object that implements a given interface but it seems the C# is unable to find the correct method.
    many thanks

  13. Joey Says:

    This is great, thanks a lot for the example.

  14. Jen0s Says:

    good code,

  15. AnonCoward Says:

    Thanks – you did help me. It took me an hour to find this.

  16. RandomGuid Says:

    Thank YOU so MUCH, I needed this for COM and it works ******


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 52 other followers

%d bloggers like this: