c# - C#调用另一个类的函数
问题描述
我有 2 个类,一个包含函数,一个包含命令,我对此很陌生,无法弄清楚如何调用该函数。
这是我试图调用的函数
private async Task<long> GetMemberId(string members)
{
long memberID = 0;
HttpResponseMessage response = await client.GetAsync(StaticObjects.bungieBasePath
+ $@"/GroupV2/Name/{memberID}/1/");
if (response.IsSuccessStatusCode)
{
try
{
dynamic content = response.Content.ReadAsAsync<ExpandoObject>().Result;
Debug.WriteLine(await response.Content.ReadAsStringAsync());
}
catch
{
throw new ArgumentException("The member could not be found.");
}
}
else
{
throw new ArgumentException("An error occurred retrieving the members information.");
}
return memberID;
}
然后这是命令
[Command("invite")]
[RequireContext(ContextType.Guild, ErrorMessage = "This command is specific to a particular server so you must send it from a channel within that server")]
public async Task SendInviteAsync()
{
await Context.Channel.TriggerTypingAsync(new RequestOptions() { Timeout = 30 });
if (!Context.IsPrivate) await Context.Message.DeleteAsync();
if (StaticObjects.CheckUserIsAdmin(Context))
{
//command to call the memberid function
}
}
解决方案
GetMemberId
是一个私有函数,只能在其类中使用。您需要将其公开,然后从其他类中调用它,如下所示:
if (StaticObjects.CheckUserIsAdmin(Context))
{
var class = new firstClass();
var memberId = class.GetMemberId(members);
}
推荐阅读
- javascript - 附加选择的多张照片的来源列出以便在上传之前可以使用 imglist[i] 显示它们
- android - 如何从 android studio 安装两个不同版本的应用程序?
- javascript - 为什么使用 display none 和 transition-delay 显示内容不起作用?
- html - 如何在具有叠加层的背景图像上制作透明导航栏?
- microsoft-graph-api - Microsoft Graph for sharepoint 中的放弃 API 提供不支持的段类型。数据查询:
- c# - 为多线程环境模拟 Dbcontext 的正确方法
- angular - 外部文件是否可以与 Angular 5+ 模块或组件通信?
- c# - 找不到类型或命名空间 AppServiceConnection
- image - 如何获得旋转图像的四个角?
- r - 调整原始数据中的小计