[英]How can I create an extension method that takes more than 2 parameters?
I have two classes in my Azure Functions project.我的 Azure Functions 项目中有两个类。
MyExtensions.cs: MyExtensions.cs:
namespace My.Functions
{
public static class MyExtensions
{
public static async Task<(bool addedcontactmail, string errorline1, string errorline2, string errorline3, string errorline4)> AddEmail(this string emailaddress, PlayFabClientInstanceAPI clientAPI, string language)
{
bool addedcontactmail = false;
string errorline1 = string.Empty;
string errorline2 = string.Empty;
string errorline3 = string.Empty;
string errorline4 = string.Empty;
var request = new PlayFab.ClientModels.AddOrUpdateContactEmailRequest();
request.EmailAddress = emailaddress;
var result = await clientAPI.AddOrUpdateContactEmailAsync(request);
if (result.Error != null)
{
errorline1 = "Error.";
errorline2 = "Please try again.";
}
else
{
addedcontactmail = true;
}
return (addedcontactmail, errorline1, errorline2, errorline3, errorline4);
}
}
}
I have tried to call the extension method AddEmail in my class NewFunstions.cs but it only seems to work if AddEmail takes only two parameters:我试图在我的类 NewFunstions.cs 中调用扩展方法 AddEmail 但它似乎只有在 AddEmail 只采用两个参数时才有效:
If MyExtensions.AddEmail only takes two parameters, then it shows a menu with the return variables:如果 MyExtensions.AddEmail 只接受两个参数,那么它会显示一个带有返回变量的菜单:
If MyExtensions.AddEmail takes three parameters:如果 MyExtensions.AddEmail 接受三个参数:
In NewFunctions.cs, I call the extension method:在 NewFunctions.cs 中,我调用了扩展方法:
var resultcontactemail = await MyExtensions.AddEmail(desiredemail, clientAPI, language);
if (resultcontactemail.)
{
functionsuccessful = resultcontactemail.addedcontactmail;
errorline1 = resultcontactemail.errorline1;
errorline2 = resultcontactemail.errorline2;
errorline3 = resultcontactemail.errorline3;
errorline4 = resultcontactemail.errorline4;
}
As you can see in the picture, it's not possible to choose the return variables addedcontactmail, errorline1, errorline2, errorline3, errorline4 from the menu if the extension method takes three parameters.如图所示,如果扩展方法采用三个参数,则无法从菜单中选择返回变量 addedcontactmail、errorline1、errorline2、errorline3、errorline4。 Why is it not showing the return variables in a menu?
为什么不在菜单中显示返回变量?
Why is my extension method not working if it takes more than two parameters?如果需要两个以上的参数,为什么我的扩展方法不起作用? Is it possible to create an extension method in Azure Functions that takes more than two parameters?
是否可以在 Azure Functions 中创建一个使用两个以上参数的扩展方法?
Remove this
inside the first parameter of AddEmail
method.在
AddEmail
方法的第一个参数中删除this
。 It will no longer be a proper extension method, but it will solve your problem.它不再是一个合适的扩展方法,但它会解决你的问题。
Continue reading if you want to know what is wrong with your extension method:如果您想知道您的扩展方法有什么问题,请继续阅读:
this string
means you are extending a string, so you must call it from an instance of string
. this string
意味着您要扩展一个字符串,因此您必须从string
的实例调用它。 The mistake you are making is that you are calling it from the class MyExtensions.AddEmail
.您犯的错误是您从
MyExtensions.AddEmail
类调用它。 I don't think it makes sense to extend a string for this so I believe my solution will put you back on the right track.我认为为此扩展字符串没有意义,所以我相信我的解决方案会让你回到正确的轨道上。 See more about it in the docs .
在文档中查看更多相关信息。 Specifically step 5.
具体步骤5。
- Call the methods as if they were instance methods on the type
调用方法,就好像它们是类型上的实例方法一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.