0

I'm trying to use an api and I can't call any methods of this class

    namespace Ipfs.Api
{

    /// <summary>
    ///   Manages the files/directories in IPFS.
    /// </summary>
    /// <remarks>
    ///   <para>
    ///   This API is accessed via the <see cref="IpfsClient.FileSystem"/> property.
    ///   </para>
    /// </remarks>
    /// <seealso href="https://github.com/ipfs/interface-ipfs-core/tree/master/API/files">Files API</seealso>
    public class FileSystemApi
    {
        IpfsClient ipfs;
        Lazy<DagNode> emptyFolder;

        internal FileSystemApi(IpfsClient ipfs)
        {
            this.ipfs = ipfs;
            this.emptyFolder = new Lazy<DagNode>(() => ipfs.Object.NewDirectoryAsync().Result);
        }

        /// <summary>
        ///   Add a file to the interplanetary file system.
        /// </summary>
        /// <param name="path"></param>
        public Task<FileSystemNode> AddFileAsync(string path)
        {
            return AddAsync(
                new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read),
                Path.GetFileName(path));
        }
}

I want to use the API without modifying it, I can't declare an object because I can't call any constructors, and I can't call any of the methods without an object because they are not static.

Error while declaring the object

What should I do?

Enrique Alcazar
  • 381
  • 7
  • 21
  • What happens if you call `var fsa = new Ipfs.Api.FileSystemApi()`? – Dour High Arch Mar 19 '17 at 18:44
  • If there is no constructor defined by you, then there is always a default constructor without parameters defined by the compiler. So you can always call new FileSystemApi(). The only way to impede this is defining a private or internal constructor inside the class FileSystemApi. In your case the designer of FIleSystemApi doesn't want anyone outside the file to be able to call new on this class. So no way to call create yourself an instance of that class – Steve Mar 19 '17 at 18:45
  • Unless you use reflection http://stackoverflow.com/questions/2023193/c-sharp-instantiating-internal-class-with-private-constructor – Steve Mar 19 '17 at 18:49
  • @DourHighArch I just posted a picture related to both answers, I'm not sure why but it doesn't let me use the default constructor without parameters. – Enrique Alcazar Mar 19 '17 at 18:51
  • @Steve then, how can I use the methods of that class? – Enrique Alcazar Mar 19 '17 at 18:52
  • 2
    If the designer of the class has decided to keep things private then you shouldn't use that class directly. Probably there is another class that uses those methods and present you with a public interface to this class. – Steve Mar 19 '17 at 18:53
  • @GrantWinney you got it, I didn't find any Filesystem class, but the IpfsClient has an object type FileSystemApi called FileSystem, and I can call it from there by creating an IpfsClient object thank you so much. – Enrique Alcazar Mar 19 '17 at 19:04

1 Answers1

2

You can't instantiate that class without reflection, which is a bit hacky (depending on an internal interface is suboptimal because API maintainers typically don't care about keeping internal bits backwards compatible.

The doc tag gives you an important clue: "This API is accessed via the IpfsClient.FileSystem property. "

RQDQ
  • 15,461
  • 2
  • 32
  • 59
  • You are right too, the correct way to access those methods is to create an IpfsClient object and then call the methods through its attribute FileSystem. – Enrique Alcazar Mar 19 '17 at 19:13
  • @GrantWinney solved my question first but it was on the comments, should I mark you as correct or wait for him to create an answer? What's the correct way to act? – Enrique Alcazar Mar 19 '17 at 19:15
  • I didn't see grant's comment before answering, but he was first and I don't feel that I added anything additional. Grant should convert his comment to an answer. – RQDQ Mar 19 '17 at 19:19