简体   繁体   English

使用功能 <T,TResult> C#

[英]Using Func<T,TResult> c#

I am working on PCL which along with a lot of functionality also provides log in. Also I recently started using delegates. 我正在使用PCL,该PCL具有许多功能,还提供了登录功能。最近我也开始使用委托。

The Current Case: 当前案例:

I have a Simple methods that takes in parameters and provide GPlus, Facebook or Simple logIn with Username and Password. 我有一个使用参数的简单方法,并提供GPlus,Facebook或带有用户名和密码的简单登录。 as in the code below: 如下面的代码:

public async Task<SignInResponse> SignInUsingFacebook(IFacebookInitializer faceBookConfiguration)
{
   //my code
}
public async Task<SignInResponse> SignInUserNameAndPass(string username,string password)
{
   //my code
}
public async Task<SignInResponse> SignInUsingGPlus(IGPlusInitializer gPlusConfiguration)
{
   //my code
}

This is great, I can call them from my application directly and handle data but, I recently came across Delegates and Func<T,TResult> 太好了,我可以直接从应用程序中调用它们并处理数据,但是最近遇到了DelegatesFunc<T,TResult>

What I want to do: 我想做的事:

I want a simple method that takes in a parameter an enum of LogInType as below: 我想要一个简单的方法,该方法接受一个参数LogInType的枚举,如下所示:

public enum LogInType
{
    UserNameAndPassword,
    Facebook,
    GooglePlus,
}

and based on the type of login type provided to the method, handles the LogIn process on it's own and just returns the SignInResponse object. 并根据提供给该方法的登录类型的类型自行处理LogIn流程,并仅返回SignInResponse对象。


Is there a way this can be done using Delegates. 有没有一种方法可以使用委托来完成。 Rather than having 3 individual methods to call for each case. 而不是为每种情况使用3个单独的方法来调用。 If yes, then can someone please help in the parameters to be passed to the Single method along with The LogInType. 如果是,那么有人可以帮忙将参数与LogInType一起传递给Single方法。 I know it has to be a Func but what would the Func look like as it has to take 3 different parameters. 我知道它必须是一个Func但是Func看起来像什么,因为它必须采用3个不同的参数。


As From the Comments: 从评论:

Each of those methods is a different signature anyway so you have a Func<IFacebookInitializer , Task<SignInResponse>> , Func<string, string, Task<SignInResponse>> and Func<IGPlusInitializer, Task<SignInResponse>> so you'd still end up with an if statement. Func<IFacebookInitializer方法都是不同的签名,因此您有一个Func<IFacebookInitializerTask<SignInResponse>>Func<string, string, Task<SignInResponse>>Func<IGPlusInitializer, Task<SignInResponse>>所以您仍然要结束通过if语句。

Can I return a Func when the method is called? 调用方法时可以返回Func吗? this ways I can return either of the three func based on a quick switch case. 这样,我可以基于快速切换的情况返回这三个函数中的任何一个。 The issue would be (if it's possible) what would be the generic response (or the common response) type of the method that returns either of these three methods 问题是(如果可能的话)返回这三个方法中的任何一个的方法的通用响应(或通用响应)类型是什么

The generic Func works as follows: The last datatype that you define in Func - "output" in this case should be the return type of the method which you are going to that particular function and the method should also be static. 通用Func的工作方式如下:在Func中定义的最后一个数据类型-在这种情况下,“输出”应该是您要转到该特定函数的方法的返回类型,并且该方法也应该是静态的。 This might help: 这可能会有所帮助:

Func<LogInType, string> Login = LoginMethod;

private static string LoginMethod(LogInType loginType)
{
    if (loginType == LogInType.Facebook)
        return "Facebook";
    if (loginType == LogInType.GooglePlus)
        return "GooglePlus";
    if (loginType == LogInType.UserNameAndPassword)
        return "UserNameAndPassword";
    return "Default";
}

The return type in the above example is string just for explaining. 上面示例中的返回类型是用于说明的字符串。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM