【发布时间】:2017-08-10 23:06:52
【问题描述】:
我正在尝试使用 api,但我无法调用此类的任何方法
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));
}
}
我想在不修改的情况下使用 API,我不能声明一个对象,因为我不能调用任何构造函数,我不能调用任何没有对象的方法,因为它们不是静态的。
我该怎么办?
【问题讨论】:
-
如果你打电话给
var fsa = new Ipfs.Api.FileSystemApi()会发生什么? -
如果没有你定义的构造函数,那么总会有一个默认的构造函数,没有编译器定义的参数。所以你可以随时调用 new FileSystemApi()。阻止这种情况的唯一方法是在 FileSystemApi 类中定义私有或内部构造函数。在您的情况下, FIleSystemApi 的设计者不希望文件之外的任何人都能够在此类上调用 new 。所以没有办法给自己创建一个该类的实例
-
@DourHighArch 我刚刚发布了一张与这两个答案相关的图片,我不知道为什么,但它不允许我使用没有参数的默认构造函数。
-
如果类的设计者决定保持私有,那么你不应该直接使用该类。可能还有另一个类使用这些方法并为您提供该类的公共接口。
标签: c# .net declaration ipfs