Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reflection with Dynamic Objects (System.__ComObject)

Reflection with Dynamic Objects (System.__ComObject)

Scheduled Pinned Locked Moved C#
csharpcomhelptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tony Richards
    wrote on last edited by
    #1

    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 a ContactItem 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 being null, 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() or typeof(ContactItem).GetProperties() returns an empty list, as does GetMembers(). Does anyone know how to achieve this with a dynamic object? I am using .Net 4

    P 1 Reply Last reply
    0
    • T Tony Richards

      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 a ContactItem 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 being null, 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() or typeof(ContactItem).GetProperties() returns an empty list, as does GetMembers(). Does anyone know how to achieve this with a dynamic object? I am using .Net 4

      P Offline
      P Offline
      Paul Michalik
      wrote on last edited by
      #2

      You 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups