我是C#中的新手,这是我第一次使用这个API
我想从API中获取Mega帐户信息。
我可以使用代码blow (TotalQuota,UsedQuota,= "TotalQuota - UsedQuota")获得(并且可以将其转换为GB)。
MegaApiClient myMegaClient = new MegaApiClient();
public void megaAccLogIn(string megaAccUserName,string megaAccPassword)
{
if (myMegaClient.IsLoggedIn == true)
{
return;
}
else
{
myMegaClient.Login(megaAccUserName, megaAccPassword);
}
}
public struct AccountInfo : IAccountInformation
{
public long TotalQuota { get; set; }
public long UsedQuota { get; set; }
public IEnumerable<IStorageMetrics> Metrics { get; set; }
public long AvailableQuota;
}
public AccountInfo getfoldercount()
{
AccountInfo myAccountInformation = new AccountInfo();
megaAccLogIn(megaAccUserName, megaAccPassword);
var myMegaClintGetAccInfo = myMegaClient.GetAccountInformation();
//IEnumerable<IStorageMetrics> test = myMegaClintGetAccInfo.Metrics;
myAccountInformation.TotalQuota = myMegaClintGetAccInfo.TotalQuota;
myAccountInformation.UsedQuota = myMegaClintGetAccInfo.UsedQuota;
myAccountInformation.AvailableQuota = myMegaClintGetAccInfo.TotalQuota - myMegaClintGetAccInfo.UsedQuota;
myAccountInformation.Metrics = myMegaClintGetAccInfo.Metrics;
return myAccountInformation;
}
这是运行该方法的代码。
private void button5_Click(object sender, EventArgs e)
{
var myAccountInformation = getfoldercount();
textBox1.Text = FormatBytes(myAccountInformation.TotalQuota) + "\r\n" + FormatBytes(myAccountInformation.UsedQuota) + "\r\n" + myAccountInformation.Metrics;
}
图片运行"myAccountInformation.Metrics“时遇到的错误
这是我为图片寻找的数据
我得到了一个异常试着用最后一种或另一种方式得到这个前男友
"Metrics“有一些重要的数据,我想要得到它,但我不能将代码作为字符串来实现。
我想要(FoldersCount,FilesCount,BytesUsed,NodeId)
我试过每一次寻找样本,但都没有发现。
我从"Github“中获得了这个信息,但我仍然不知道如何使用它,以及接口是什么。我不需要在C#上上一课,只需要一点关于正在发生的事情的洞察力,这样我就可以继续了。
如果有人愿意帮助我,我将心存感激。
发布于 2018-05-17 15:20:32
根据您向MegaApiClient
提供的链接,您没有正确地设置AccountInfo
。我可能错了;这个答案只基于您提供的内容和指向源MegaApiClient
的链接,我认为它应该是匹配的。
我首先编写我的AccountInfo
以匹配IAccountInformation
接口,如下所示:
public struct AccountInfo : IAccountInformation
{
public long TotalQuota { get; set; }
public long UsedQuota { get; set; }
public IEnumerable<IStorageMetrics> Metrics { get; set; }
}
我相信,只要改变这一点,就能弥补你所犯的错误。在您的代码中,您指出Metrics
是一个long
,而实际上它需要是IEnumerable<IStorageMetrics>
。如果你需要进一步解释的话,请告诉我,我很乐意发帖/解释更多。
既然你问到关于Interface
的洞察力,我会尽量简短地解释它。
Interface
基本上是object
应该是什么样子的蓝图。它解释了object
的特性。例如:在IAccountInformation
接口(通过您提供的链接)中,它声明一个对象将至少具有一个只读属性long TotalQouta
。它还声明了另外两个属性long UserQouta
和IEnumerable<IStorageMetrics> Metrics
。
因此,如果您制作了一个使用此object
的Interface
,则必须实现这些特性以匹配Interface
。您将看到我为您将Interface
添加到struct
中,并精确地实现了它。(我也向属性添加了setter,因为您正在以这种方式使用它们。)
有趣的是多态的使用。现在可以通过直接类型或IAccountInformation
接口作为类型访问对象。这对于那些只知道Interface
而不知道要自己编写的Type
(或‘对象)的方法很有用。
另一件有趣的事情是多态已经在IAccountInformation
Interface
中被使用了。property
IEnumerable<IStoreMetrics> Metrics { get; }
声明,从IAccountInformation
派生的任何Type
也将具有从IStorageMetrics
派生的类型的枚举。
当我们查看IStorageMetrics
(通过您提供的链接)时,您将看到4个属性(NodeId、BytesUsed、FilesCount、FoldersCount)。
代码中的Interface
类似于娱乐接收器、电视或计算机背面的端口。你有HDMI,RCA,Optic,USB等等.如果你有一个有RCA和Optic的接收器,那么你知道你可以将RCA电缆插入其中,也可以将光纤电缆插入其中,因为这是接收器的接口。RCA插孔是用来做一些特定的事情,但是接收方不知道你要插入什么。它只知道它必须与那个界面匹配。密码是一样的..。我们不知道是谁在使用IAccountInformation
Interface
,但我们知道它将有IAccountInformation
所说的设置,我们可以通过这些设置与其通信。
Ok,所以您已经更新了这个问题--一些是很好的。我将留下我已经发布的内容,并在这里发布更多内容。
不要忘记Metrics
是IEnumerable<IStorageMetrics>
,这意味着它是IStorageMetrics
类型的枚举。(如果它有助于可视化,可以将Metrics
看作是一个数组或IStorageMetrics
列表。IEnumerable
只是意味着可以对类型进行枚举。
枚举可以以多种方式使用,但其中最常见的一种是循环(如foreach
循环)。因此,例如,您可以像这样从Metrics
获得信息。
foreach (var storageMetric in myAccountInformation.Metrics)
{
var bytesUsed = storageMetric.BytesUsed;
var filesCount = storageMetric.FilesCount;
var foldersCount = storageMetric.FoldersCount;
var nodeId = storageMetric.NodeId;
// do what you want with the info here
}
IEnumerable
的另一个流行用法是使用System.Linq
,它使用IEnumerable
作为所有查询的核心。你可以用linq,就像你以前那样,就像.
// The following will result in totalBytes being the sum of all Metrics
var totalBytes = myAccountInformation.Metrics.Sum(storageMetric => storageMetric.BytesUsed);
因此,您可以使用以下内容编写文本:(当然,如果您想要的值是BytesUsed之和。)
var totalBytesUsed = myAccountInformation.Metrics.Sum(storageMetric => storageMetric.BytesUsed);
textBox1.Text = FormatBytes(myAccountInformation.TotalQuota) + "\r\n" + FormatBytes(myAccountInformation.UsedQuota) + "\r\n" + totalBytesUsed;
这说得通吗?
https://stackoverflow.com/questions/50394454
复制相似问题