locked
Are "Extension" Methods accessible? RRS feed

  • Question

  • Forgive me if in my asking a question it's clear I don't have a clue what I'm doing, because I mostly don't. :)

    I'm trying to use the TriNet.Core.IO.Ntfs.dll found at http://www.codeproject.com/KB/cs/ntfsstreams.aspx to access alternate data streams.  I can load the assembly without error and call it's static methods (e.g. [Trinet.Core.IO.Ntfs]::ListAlternateDataStreams() ), but some of the functionality the dll adds are extensions to existing classes.  These don't work and I'm wondering if it's simply something I'm doing wrong or if it's not supported.

    The example in the link shows the following code fragment:

     

    using
    
     System.IO;
    using
    
     Trinet.Core.IO.Ntfs;
    ...
    FileInfo file = new FileInfo(path);
    
    // List the additional streams for a file:
    foreach (AlternateDataStreamInfo s in file.ListAlternateDataStreams())
    {
     Console.WriteLine("{0} - {1} bytes", s.Name, s.Size);
    }

    The call to file.ListAlternativeDataStreams() is an extension method, extending the standard IO.FileInfo class, but this call fails within PowerShell.  Is there something else I need to do to make these extension methods work, or is this just not supported within PowerShell?

    Thanks in advance,

    Tim

     

    • Edited by wtgreen Wednesday, March 9, 2011 4:50 PM code formatting
    Wednesday, March 9, 2011 4:48 PM

Answers

  • Create FileStream.ps1xml:

     

    <?xml version="1.0" encoding="utf-8" ?> 
    <Types> 
     <Type> 
      <Name>System.IO.FileInfo</Name> 
      <Members> 
       <ScriptMethod> 
        <Name>ListAlternateDataStreams</Name> 
         <Script> 
         [Trinet.Core.IO.Ntfs.FileSystem]::ListAlternateDataStreams($this.fullname) 
        </Script> 
       </ScriptMethod> 
      </Members> 
     </Type> 
    </Types>
    

     

    Update-TypeData C:\FileStream.ps1xml

    (get-item C:\windows\win.ini).
    ListAlternateDataStreams()

     


    • Proposed as answer by Kazun Wednesday, March 9, 2011 6:15 PM
    • Marked as answer by wtgreen Wednesday, March 9, 2011 7:38 PM
    Wednesday, March 9, 2011 5:24 PM

All replies

  • [Trinet.Core.IO.Ntfs.FileSystem]::ListAlternateDataStreams($file)
    Wednesday, March 9, 2011 4:57 PM
  • Yes, as I mentioned I can call it's static methods, but not it's extension methods.
    Wednesday, March 9, 2011 5:07 PM
  • what are extension methods? can you give a C# example?

    Wednesday, March 9, 2011 5:18 PM
  • Create FileStream.ps1xml:

     

    <?xml version="1.0" encoding="utf-8" ?> 
    <Types> 
     <Type> 
      <Name>System.IO.FileInfo</Name> 
      <Members> 
       <ScriptMethod> 
        <Name>ListAlternateDataStreams</Name> 
         <Script> 
         [Trinet.Core.IO.Ntfs.FileSystem]::ListAlternateDataStreams($this.fullname) 
        </Script> 
       </ScriptMethod> 
      </Members> 
     </Type> 
    </Types>
    

     

    Update-TypeData C:\FileStream.ps1xml

    (get-item C:\windows\win.ini).
    ListAlternateDataStreams()

     


    • Proposed as answer by Kazun Wednesday, March 9, 2011 6:15 PM
    • Marked as answer by wtgreen Wednesday, March 9, 2011 7:38 PM
    Wednesday, March 9, 2011 5:24 PM
  • what are extension methods? can you give a C# example?

    They are methods added to an existing base type.  They're explained well here: http://msdn.microsoft.com/en-us/library/bb383977.aspx
    Wednesday, March 9, 2011 6:12 PM
  • Awesome Kazun... that's just what I was looking for!
    Wednesday, March 9, 2011 6:13 PM
  • PowerShell doesn't support extension methods.

    However an extension method is just a static method with an extra attribute, so just call as a static method passing the subject object as the first parameter.


    Richard J Cox
    Friday, March 11, 2011 12:38 PM