简体   繁体   中英

How can I create an object of a class with no constructor, C#

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.

声明对象时出错

What should I do?

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. "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM