我有一个从我的应用程序中调用的方法。方法是从实现的Google.Apis.Util.Store.IDataStore
。我想从该方法返回令牌,但我不知道如何。令牌是类类型TokenResponse。如何从这样的方法返回类TokenResponse?
public Task<T> GetAsync<T>(string key)
{
//TokenResponse token = new TokenResponse();
//token.RefreshToken = @"rere545454545";
//return token; -- I would like to return this, but it throws error
return Task.FromResult<T>(default); //that works, but not much help out of it
}
更新资料
基本上,我想返回自己的令牌,该令牌已经在数据库中了。但是尝试创建一个新实例,例如:
TokenResponse token = new TokenResponse()
抛出和错误
无法创建变量类型TokenResponse的实例,因为它没有new()约束
解决问题的最简单方法是使用下一种方法:
public Task<T> GetAsync<T>(string key)
{
TokenResponse t = new TokenResponse();
// ...
return Task.FromResult((T) ((object) t));
}
您应确保GetAsync<T>
始终使用type参数调用该方法T = TokenResponse
。否则,此代码将抛出InvalidCastException
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句