Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取和写入隔离存储器的通用等待方法_C#_Generics_Asynchronous_Async Await_Isolatedstorage - Fatal编程技术网

C# 读取和写入隔离存储器的通用等待方法

C# 读取和写入隔离存储器的通用等待方法,c#,generics,asynchronous,async-await,isolatedstorage,C#,Generics,Asynchronous,Async Await,Isolatedstorage,我试图创建两种方法来使用PCLStorage写入和读取隔离存储,但读取(Get)部分存在一些问题: public异步静态任务GetObject() { IFolder rootFolder=FileSystem.Current.LocalStorage; IFolder folder=wait rootFolder.CreateFolderAsync(“缓存”,CreationCollisionOption.OpenIfExists); IFile file=wait folder.GetFil

我试图创建两种方法来使用PCLStorage写入和读取隔离存储,但读取(Get)部分存在一些问题:

public异步静态任务GetObject()
{
IFolder rootFolder=FileSystem.Current.LocalStorage;
IFolder folder=wait rootFolder.CreateFolderAsync(“缓存”,CreationCollisionOption.OpenIfExists);
IFile file=wait folder.GetFileAsync(T.GetType().Name);
返回wait file.ReadAllTextAsync();
}
公共异步静态void SetObject(T obj)
{
IFolder rootFolder=FileSystem.Current.LocalStorage;
IFolder folder=wait rootFolder.CreateFolderAsync(“缓存”,CreationCollisionOption.OpenIfExists);
wait folder.CreateFileAsync(obj.GetType().Name,CreationCollisionOption.ReplaceExisting);
}

我在这里要做的是为我的对象创建两个通用函数,它们是我想要存储在独立存储中的。我的问题是,当GetObject返回
Task
,例如
Task
时,我一直在思考如何“genericfy”GetObject。通常
T
是用方法名定义的,但这里它需要用任务来定义。

它仍然工作相同,您必须在方法名后定义T:

public async static Task<T> GetObject<T>()
public异步静态任务GetObject()
如果你有,你可以这样称呼它:

SomeType obj = await GetObject<SomeType>();
SomeType obj=await GetObject();

尽管这不能直接用于当前代码,但您必须以某种方式将
ReadAllTextAsyc
返回的内容(可能是
string
)转换为
t

,顺便说一句,异步方法的名称应以
-Async
结尾,例如
GetObjectAsync
SomeType obj = await GetObject<SomeType>();