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.
February 16, 2010 at 10:44 PM
C#: Using reflection with COM objects « Mehroz’s Experiments…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
March 18, 2010 at 12:55 AM
NOt work
propertyValue have comobject and not propertys of it
March 26, 2010 at 4:07 PM
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.
April 16, 2010 at 1:22 PM
Hi Mehroz, this is great but how can I then Set the value for the property?
April 16, 2010 at 2:46 PM
Hi David,
The code under the comment:
//set the value of comObject.PropertyNamein the post can be used to set the property. Do you have any problems with that?April 16, 2010 at 3:08 PM
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?
April 16, 2010 at 3:52 PM
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; } } }April 16, 2010 at 4:33 PM
int UniqueID { get; }
Member of Microsoft.Office.Interop.MSProject.Resource
April 16, 2010 at 4:39 PM
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, PublicPropertyPrivateSetcan be written using reflection except:ReadOnlyField.Hope this answers your question
August 12, 2010 at 10:10 PM
THANK YOU!!!!!
April 19, 2011 at 6:28 PM
Thank you,
you save me lot of time
May 5, 2011 at 3:15 PM
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
July 9, 2011 at 1:31 AM
This is great, thanks a lot for the example.
December 8, 2011 at 4:27 AM
good code,
May 28, 2012 at 10:23 AM
Thanks – you did help me. It took me an hour to find this.
October 10, 2012 at 7:28 AM
Thank YOU so MUCH, I needed this for COM and it works ******