title: C# 4.0 dynamic object and WinAPI interfaces like IShellItem (without defining them in the C# source)
body: <p>Is it possible (with the new dynamic keyword in C# 4.0) to use interfaces (like IShellItem or other WinAPI interfaces) without defining them in my C# source code? Or at least not define the interface members?</p>
  
  <p>I'm trying something like:</p>
  
  <pre><code>        const string IShellItemGuid = "43826D1E-E718-42EE-BC55-A1E261C37BFE";
          Guid guid = new Guid(IShellItemGuid);
          dynamic nativeShellItem = null;
  
          // Create and initializes a Shell item object (IShellItem) from a parsing name
          SHCreateItemFromParsingName(@"M:\TEST.TXT", IntPtr.Zero, guid, out nativeShellItem);
          if (nativeShellItem != null)
          {
              MessageBox.Show(nativeShellItem.GetDisplayName(0));
          }
  </code></pre>
  
  <p>where</p>
  
  <pre><code>    [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
      static extern void SHCreateItemFromParsingName(
      [In][MarshalAs(UnmanagedType.LPWStr)] string pszPath,
      [In] IntPtr pbc,
      [In][MarshalAs(UnmanagedType.LPStruct)] Guid iIdIShellItem,
      [Out][MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] out dynamic iShellItem);
  </code></pre>
  
  <p>The <em>SHCreateItemFromParsingName</em> works fine and I'm getting an object if the file exists (and a correct error message if the file does not exist), however, trying to call <em>nativeShellItem.GetDisplayName</em> gives me a runtime exception:</p>
  
  <blockquote>
    <p>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled: 'System.__ComObject' does not contain a definition for 'GetDisplayName'</p>
  </blockquote>
  
  <p>although <em>IShellItem</em> has a method called <em>GetDisplayName</em>, and my <em>nativeShellItem</em> dynamic object returned by <em>SHCreateItemFromParsingName</em> should implement this interface.</p>