如何实现返回类型为Task<;T>;

本文关键字:Task gt lt 返回类型 何实现 实现 | 更新日期: 2023-09-27 17:57:49

我正试图为新的身份系统开发一种数据库优先的方法,但我目前有一点被难住了。这就是我迄今为止拥有的

    public class MyCustomUserStore<T>: IUserStore<T> where T:ApplicationUser
    {
         public System.Threading.Tasks.Task<T> FindByIdAsync(string userId)
        {
            Task<ApplicationUser> taskInvoke = Task<ApplicationUser>.Factory.StartNew(() =>
            {
                //Here is where I will lookup my existing legacy database table
                //setting some dummy value here to the properties added tothe ApplicationUser class
                ApplicationUser obj = new ApplicationUser();
                obj.Id = "userid : " + userId;
                return  obj;
            });
            return  taskInvoke;
        }
     }

我得到的错误是

Cannot implicitly convert type 'System.Threading.Tasks.Task<MyProject.Models.ApplicationUser>' 
    to 'System.Threading.Tasks.Task<T>'

问题1.)我的类定义正确吗?如果是,如何进一步获取此方法返回的值?

问题2.)我应该为实现这个类的方法而烦恼吗?或者,如果我只是扩展UserManager类并覆盖它的所有方法,会不会更好

   public class MyCustomUserManager : UserManager<ApplicationUser>
   {
   }

如何实现返回类型为Task<;T>;

这样的东西应该能在中工作

public System.Threading.Tasks.Task<T> FindByIdAsync(string userId)
        {
                var obj = new T();
                obj.Id = "userid : " + userId;
                return Task.FromResult(T);
        }