Reflection with Dynamic Objects (System.__ComObject)
-
I am trying to access a property on a System.__ComObject by name. I can do this for normal objects:
var mapping = m.GoogleMapping as SimpleGoogleMapping;
var propInfo = googleContact.GetType().GetProperty(mapping.PropertyName);
googleValue = propInfo.GetValue(googleContact, null);googleContact
is a normal class so this doesn't cause a problem. I tried this (outlookContact
is aContactItem
retreived using Outlook Interop, but during debugging, shows as a System.__ComObject):var mapping = m.OutlookMapping as SimpleOutlookMapping;
var propInfo = outlookContact.GetType().GetProperty(mapping.PropertyName);
propInfo.SetValue(outlookContact, googleValue, null);But
propInfo
ends up beingnull
, even though the property name is correct. I've also tried:var propInfo = typeof(ContactItem).GetProperty(mapping.PropertyName);
without success. Edit: Using
outlookContact.GetType().GetProperties()
ortypeof(ContactItem).GetProperties()
returns an empty list, as doesGetMembers()
. Does anyone know how to achieve this with a dynamic object? I am using .Net 4 -
I am trying to access a property on a System.__ComObject by name. I can do this for normal objects:
var mapping = m.GoogleMapping as SimpleGoogleMapping;
var propInfo = googleContact.GetType().GetProperty(mapping.PropertyName);
googleValue = propInfo.GetValue(googleContact, null);googleContact
is a normal class so this doesn't cause a problem. I tried this (outlookContact
is aContactItem
retreived using Outlook Interop, but during debugging, shows as a System.__ComObject):var mapping = m.OutlookMapping as SimpleOutlookMapping;
var propInfo = outlookContact.GetType().GetProperty(mapping.PropertyName);
propInfo.SetValue(outlookContact, googleValue, null);But
propInfo
ends up beingnull
, even though the property name is correct. I've also tried:var propInfo = typeof(ContactItem).GetProperty(mapping.PropertyName);
without success. Edit: Using
outlookContact.GetType().GetProperties()
ortypeof(ContactItem).GetProperties()
returns an empty list, as doesGetMembers()
. Does anyone know how to achieve this with a dynamic object? I am using .Net 4You cannot query a COM wrapper for CLR properties since it does not have any. Have you tried to extract the properties directly from the assembly which defines the COM CoClass? Something like this:
using System.Reflection;
//... somewhere
const string sOutlookInteropAssemblyName =
"Microsoft.Office.Interop.Outlook";
const string sContactItemTypeName =
"_ContactItem";//... somewhere (error handling ommitted)
PropertyInfo FindProperty(string pPropertyName) {
Assemby tOulookInterop =
Assembly.ReflectionOnlyLoad(sOutlookInteropAssembly);
Type tContactItemType =
tOutlookInterop.GetType(sContactItemTypeName);// this is what you want I assume...
PropertyInfo [] tProperties =
tContactItemType.GetProperties();return tProperties.Find(Property => Property.Name == pPropertyName);
}Huch, there is a lot of stuff which can go wrong, I just typed in the code without checking it, but I hope you get the point.
modified on Saturday, August 28, 2010 4:28 AM