如何将 ObservableCollection<SomeType> 转换回 ObservableCollection<T>?
How to convert ObservableCollection<SomeType> back to ObservableCollection<T>?
我正在努力在具有 return 类型的泛型方法中转换回泛型集合类型。
在this answer中写的很清楚,避免
switching on a type in a generic
但在 this answer 中解释了如何操作。
我承认,我使用泛型来避免为三种不同的 return 类型编写三次相同的代码。现在编译器告诉我,
not all code paths return a value
调用方方法示例:
public ObservableCollection<LoadedJockey> Jockeys { get; private set; }
Jockeys = await _updateDataService.UpdateDataAsync(Jockeys, DataUpdateModules.JPlFrom, DataUpdateModules.JPlTo, "updateJockeysPl");
我的通用方法如下所示:
public async Task<ObservableCollection<T>> UpdateDataAsync<T>(ObservableCollection<T> genericCollection, int idFrom, int idTo, string jobType) where T : IConvertible
{
//variables reset here
_loopCounterProgressBar = 0;
_idFromProgressBar = idFrom;
_idToProgressBar = idTo;
if (typeof(T) == typeof(LoadedHorse))
{
//do something here
}
else if (typeof(T) == typeof(LoadedJockey))
{
//do something here
}
else if (typeof(T) == typeof(LoadedHistoricalRace))
{
//do something here
}
//initial
SemaphoreSlim throttler = new SemaphoreSlim(_degreeOfParallelism);
List<Task> tasks = new List<Task>();
TokenSource = new CancellationTokenSource();
CancellationToken = TokenSource.Token;
OnProgressBarTick();
//run loop
for (int i = idFrom; i < idTo; i++)
{
int id = i;
tasks.Add(Task.Run(async () =>
{
try
{
if (CancellationToken.IsCancellationRequested)
return;
await throttler.WaitAsync(TokenSource.Token);
if (jobType.Contains("Horses"))
{
await //call service method
}
else if (jobType.Contains("Jockeys"))
{
await //call service method
}
else if (jobType.Contains("Historic"))
{
await //call service method
}
}
catch (Exception e)
{
}
finally
{
_loopCounterProgressBar++;
EventHandler<UpdateBarEventArgs> progressBarTick = _updateProgressEventHandler;
OnProgressBarTick();
throttler.Release();
}
}));
}
try
{
await Task.WhenAll(tasks);
}
catch (OperationCanceledException)
{
}
finally
{
//save results in file when finish
}
}
//here I wanted to return some result depending on T
if (typeof(T) == typeof(LoadedHorse))
{
return (ObservableCollection<T>)Convert.ChangeType(Horses, typeof(ObservableCollection<T>));
}
else if (typeof(T) == typeof(LoadedJockey))
{
return (ObservableCollection<T>)Convert.ChangeType(Jockeys, typeof(ObservableCollection<T>));
}
else if (typeof(T) == typeof(LoadedHistoricalRace))
{
return (ObservableCollection<T>)Convert.ChangeType(Races, typeof(ObservableCollection<T>));
}
}
如您所见,我希望收到 3 种不同类型的 T。我认为我涵盖了所有这些。我的解决方案基于 this example。我猜,我可能 Convert
错误地将我的类型输入 T
,但我不应该怎么做。
按照 Camilo Terevinto 的建议,我在 if else
链的末尾添加了 else { throw new ArgumentException() }
,编译器很高兴。
我正在努力在具有 return 类型的泛型方法中转换回泛型集合类型。
在this answer中写的很清楚,避免
switching on a type in a generic
但在 this answer 中解释了如何操作。
我承认,我使用泛型来避免为三种不同的 return 类型编写三次相同的代码。现在编译器告诉我,
not all code paths return a value
调用方方法示例:
public ObservableCollection<LoadedJockey> Jockeys { get; private set; }
Jockeys = await _updateDataService.UpdateDataAsync(Jockeys, DataUpdateModules.JPlFrom, DataUpdateModules.JPlTo, "updateJockeysPl");
我的通用方法如下所示:
public async Task<ObservableCollection<T>> UpdateDataAsync<T>(ObservableCollection<T> genericCollection, int idFrom, int idTo, string jobType) where T : IConvertible
{
//variables reset here
_loopCounterProgressBar = 0;
_idFromProgressBar = idFrom;
_idToProgressBar = idTo;
if (typeof(T) == typeof(LoadedHorse))
{
//do something here
}
else if (typeof(T) == typeof(LoadedJockey))
{
//do something here
}
else if (typeof(T) == typeof(LoadedHistoricalRace))
{
//do something here
}
//initial
SemaphoreSlim throttler = new SemaphoreSlim(_degreeOfParallelism);
List<Task> tasks = new List<Task>();
TokenSource = new CancellationTokenSource();
CancellationToken = TokenSource.Token;
OnProgressBarTick();
//run loop
for (int i = idFrom; i < idTo; i++)
{
int id = i;
tasks.Add(Task.Run(async () =>
{
try
{
if (CancellationToken.IsCancellationRequested)
return;
await throttler.WaitAsync(TokenSource.Token);
if (jobType.Contains("Horses"))
{
await //call service method
}
else if (jobType.Contains("Jockeys"))
{
await //call service method
}
else if (jobType.Contains("Historic"))
{
await //call service method
}
}
catch (Exception e)
{
}
finally
{
_loopCounterProgressBar++;
EventHandler<UpdateBarEventArgs> progressBarTick = _updateProgressEventHandler;
OnProgressBarTick();
throttler.Release();
}
}));
}
try
{
await Task.WhenAll(tasks);
}
catch (OperationCanceledException)
{
}
finally
{
//save results in file when finish
}
}
//here I wanted to return some result depending on T
if (typeof(T) == typeof(LoadedHorse))
{
return (ObservableCollection<T>)Convert.ChangeType(Horses, typeof(ObservableCollection<T>));
}
else if (typeof(T) == typeof(LoadedJockey))
{
return (ObservableCollection<T>)Convert.ChangeType(Jockeys, typeof(ObservableCollection<T>));
}
else if (typeof(T) == typeof(LoadedHistoricalRace))
{
return (ObservableCollection<T>)Convert.ChangeType(Races, typeof(ObservableCollection<T>));
}
}
如您所见,我希望收到 3 种不同类型的 T。我认为我涵盖了所有这些。我的解决方案基于 this example。我猜,我可能 Convert
错误地将我的类型输入 T
,但我不应该怎么做。
按照 Camilo Terevinto 的建议,我在 if else
链的末尾添加了 else { throw new ArgumentException() }
,编译器很高兴。