none
How to get list of properties of dynamic object ? RRS feed

  • Question

  • I have dynamically created Outlook COM object:
    Type outlookType = Type.GetTypeFromProgID( "Outlook.Application" );
    dynamic outlookApp = Activator.CreateInstance( outlookType );

    Now I need to get properties of outlookApp object.

    Is it possible ?

    It seems that it does not implement IDynamicMetaObjectProvider interface.


    Oleg Subachev

    Monday, August 25, 2014 12:35 PM

Answers

  • For COM objects you can try to use property descriptors:

    var outlookApp = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));
    
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(outlookApp)) {
        Console.WriteLine(prop.Name);
    }
    

    Wednesday, August 27, 2014 5:48 AM
    Moderator

All replies

  • You could do this using reflection:

    Type outlookType = Type.GetTypeFromProgID( "Outlook.Application" );
               dynamic outlookApp = Activator.CreateInstance( outlookType );
               foreach (var prop in outlookApp.GetType().GetProperties())
               {
                   string propName = prop.Name;
                   object value = prop.GetValue(outlookApp, null);
               }

    • Proposed as answer by Mr. Zator Monday, August 25, 2014 12:47 PM
    • Unproposed as answer by Mike DanesModerator Wednesday, August 27, 2014 5:45 AM
    Monday, August 25, 2014 12:42 PM
  • Thank you. This seems to work.

    In reality I tried to get members of ContactItem instance retrieved as:
    dynamic contact=outlookApp.Session.GetDefaultFolder(10).Items[1];
    This returns System.__ComObject instance which has no properties.

    Now using reflection step by step from the root object (outlookApp) I hope to get all info I needed.


    Oleg Subachev



    Tuesday, August 26, 2014 6:39 AM
  • I have dynamically created Outlook COM object:
    Type outlookType = Type.GetTypeFromProgID( "Outlook.Application" );
    dynamic outlookApp = Activator.CreateInstance( outlookType );

    Now I need to get properties of outlookApp object.

    Is it possible ?

    It seems that it does not implement IDynamicMetaObjectProvider interface.


    Oleg Subachev

    You should use object and not dynamic, because Activator.CreateInstance() returns object. Moreover, dynamic defeats compile time type checking.

    Your code needs reflection to use the COM object, thus, it's only good for .NET COM objects, which is not the case for Outlook (as far as I know).

    For this reason, you should add a reference to the COM object in your project.

    Tuesday, August 26, 2014 8:38 AM
  • The problem is as follows:

    var outlookApp = Activator.CreateInstance( Type.GetTypeFromProgID( "Outlook.Application" ) );
    returns Microsoft.Office.Interop.Outlook.ApplicationClass instance, members available through reflection.

    var session = outlookApp.GetType().GetProperty( "Session" ).GetValue( outlookApp );
    returns Microsoft.Office.Interop.Outlook.NameSpaceClass instance, members available through reflection.

    var folders = session.GetType().GetProperty( "Folders" ).GetValue( session, null );
    returns Microsoft.Office.Interop.Outlook.FoldersClass instance, members available through reflection.

    But now any attempt to get Folder returns not Microsoft.Office.Interop.Outlook.FolderClass instance, but System.__ComObject instance.

    To get Folder I've tried the following methods:
    var folder = session.GetType().GetMethod( "GetDefaultFolder" ).Invoke( session, new object[] { 10 } );
    var folder = folders.GetType().GetProperty( "Item" ).GetValue( folders, new object[] { 1 } );
    var folder = folders.GetType().GetMethod( "GetFirst" ).Invoke( folders, null );

    All of them return System.__ComObject instance, which represents dynamic and has only standart Object's members.


    Oleg Subachev






    Tuesday, August 26, 2014 10:44 AM
  • var folders = session.GetType().GetProperty( "Folders" ).GetValue( session, null );

    The code above compiles correctly, but does it execute without throwing exception?

    Are you sure it's not inside a try/catch?

    When using reflection, the game ends when you get System.__ComObject, because you can't cast it to the real object it is wrapping. Add a reference in your project, and you'll be able (the compiler and IntelliSense will know the types).

    • Edited by far sunrise Tuesday, August 26, 2014 1:26 PM
    Tuesday, August 26, 2014 1:17 PM
  • In my production code I use standart dynamic calls.
    All works OK.

    But my goal was to get properties of dynamic object. All is OK for 'outlookApp', 'session' and 'folders'. But as 'folder' is System.__ComObject there is no way further. You are right.


    Oleg Subachev

    Wednesday, August 27, 2014 5:32 AM
  • For COM objects you can try to use property descriptors:

    var outlookApp = Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));
    
    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(outlookApp)) {
        Console.WriteLine(prop.Name);
    }
    

    Wednesday, August 27, 2014 5:48 AM
    Moderator
  • Thanks. Yes, this returns the list of properties of Contacts Folder.

    Oleg Subachev

    Thursday, September 18, 2014 5:30 AM