gpt4 book ai didi

c# - 如何在线程上调用泛型方法?

转载 作者:太空宇宙 更新时间:2023-11-03 20:41:18 25 4
gpt4 key购买 nike

如何在线程上调用具有以下 header 的方法?

    public void ReadObjectAsync<T>(string filename)
{
// Can't use T in a delegate, moved it to a parameter.
ThreadStart ts = delegate() { ReadObjectAcync(filename, typeof(T)); };
Thread th = new Thread(ts);
th.IsBackground = true;
th.Start();
}

private void ReadObjectAcync(string filename, Type t)
{
// HOW?
}

public T ReadObject<T>(string filename)
{
// Deserializes a file to a type.
}

最佳答案

为什么你不能这样做...

public void ReadObjectAsync<T>(string filename)
{
ThreadStart ts = delegate() { ReadObject<T>(filename); };
Thread th = new Thread(ts);
th.IsBackground = true;
th.Start();
}

private void ReadObject<T>(string filename)
{
// Deserializes a file to a type.

}

关于c# - 如何在线程上调用泛型方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2528006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com