简体   繁体   English

如何创建一个包含 2 个以上参数的扩展方法?

[英]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。

  1. Call the methods as if they were instance methods on the type调用方法,就好像它们是类型上的实例方法一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中,我如何创建一个扩展方法,该方法采用带有泛型参数的Lambda函数,Func <T, TResult> 作为参数。 - In C# how do I create an extension method that takes a lambda with generic parameters, Func<T, TResult> as a parameter. 如何创建一个以2个双精度和一个时间跨度为参数的webapi方法? - How do I create a webapi method that takes in 2 doubles and a timespan as parameters? 如何创建一个多于一个Enum类型的方法,而不是我必须一直使用Cast - How can I make a method that takes more than one Enum type instead of me having to Cast all the time 如何修改此扩展方法接受两个字符串参数? - How can I modify this extension method accept two string parameters? 如何使此扩展方法更通用? - How can I make this extension method more generic? 如何从一个方法返回多个变量? - How can I return from a method more than one variable? 如何创建创建新列表并采用其他参数的方法 - How to create a Method that creates a new List and takes additional parameters 我如何为asp.net页面创建扩展方法 - How I can create an extension method for asp.net page 如何为 Microsoft.AppCenter.Crashes 创建扩展方法 - How can I create extension method for Microsoft.AppCenter.Crashes 如何实现采用基类后代的扩展方法? - How can implement an extension method that takes descendants of the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM