0

I need to access a specific property inside a COM object (the iTunes COM Library). You can access this property with the dynamic view of the Visual Studio debugger.

Visual Studio Dynamic Debug View

I tried to get this property using Reflection but I don't get any private properties or fields back.

I can access all the Properties that I also see in the debugger using this line:

new Microsoft.CSharp.RuntimeBinder.DynamicMetaObjectProviderDebugView(myObject).Items

However, I would rather not use this call because I believe an easier solution exists.

If you have iTunes installed this would be a simple example of what I'm trying to achieve:

iTunesAppClass app;
if (Process.GetProcessesByName("iTunes").Any())
{
    app = new iTunesAppClass();
}
else
{
   return;
}

foreach (IITPlaylist playlist in app.LibrarySource.Playlists)
{
    // This does not work. There is no "Parent".
    //var parent = playlist.Parent;

    Type playListType = playlist.GetType();

    // both contain 0 results
    var fields = playListType.GetFields(BindingFlags.NonPublic);
    var properties = playListType.GetFields(BindingFlags.NonPublic);

    // works but only during runtime
    //var parent2 = new Microsoft.CSharp.RuntimeBinder.DynamicMetaObjectProviderDebugView(playlist).Items[4];
}
Felix
  • 81
  • 2
  • 1
    It is not exactly hidden, you are just using a lesser interface type. Try casting the interface reference to `IITUserPlaylist`, it has a Parent property. Beware that it may fail, so use the `as` keyword and check for null. http://www.joshkunz.com/iTunesControl/interfaceIITUserPlaylist.html – Hans Passant Apr 26 '18 at 10:25
  • Thanks a lot for your help! This was the solution. It would have been a lot easier to figure this out myself if apple didn't delete the documentation. So thanks again for the link. – Felix Apr 26 '18 at 12:06
  • Also, if you definitely know a member should be there and you don't have the c# type, you can just use `dynamic`. e.g. `dynamic playlist = app.LibrarySource.Playlists[0]; dynamic parent = playlist.Parent; parent.SomeMethod();` https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic –  Apr 27 '18 at 04:54

0 Answers0