這個Transfrom函式通過某種規則將源陣列的每個元素從一種型別轉換為另一種型別。它有 2 個引數,其中一個是介面
public static TResult[] Transform<TSource, TResult>(this TSource[] source, ITransformer<TSource, TResult> transformer)
{
if (source == null || transformer == null)
{
throw new ArgumentNullException(nameof(source));
}
if (source.Length == 0)
{
throw new ArgumentException("Array is empty");
}
var res = Array.ConvertAll(source, transformer.Transform(source));
return res;
}
由于transformer.Transform(source)的引數無法從TSource[] 轉換為TSource ,因此出現錯誤
這是介面 ITransformer
public interface ITransformer<in TSource, out TResult>
{
TResult Transform(TSource obj);
}
uj5u.com熱心網友回復:
ConvertAll檔案顯示該方法具有以下簽名:
public static TOutput[] ConvertAll<TInput,TOutput> (TInput[] array, Converter<TInput,TOutput> converter);
如您所見,它需要一個Converter<TInput, TOutput>. 如果我們查看檔案,我們可以看到它是具有以下簽名的委托:
public delegate TOutput Converter<in TInput,out TOutput>(TInput input);
所以你應該能夠像這樣重寫你的轉換行:
var res = Array.ConvertAll(source, new Converter<TSource, TResult>(transformer.Transform));
在線嘗試
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/480194.html
上一篇:Springboot和Zuul代理DeferringLoadBalancerExchangeFilterFunction錯誤
