我有一個類Frame
,它包含一些樣本。我想要一個方法AsType
將這些樣本轉換爲另一種數字類型。方法接收和返回泛型類型 - 如何做?
我試過這個,但它不起作用,因爲The type or namespace 'TargetType' cannot be found
。另外,爲什麼它不起作用,我有點失落,我怎麼能想出正確的方式來思考它,並做到正確。
public class Frame<T>
{
public T[] Samples { get; protected set; }
public Frame(IEnumerable<T> Samples)
{
Samples = Samples.ToArray();
}
public Frame<TOut> AsType(Type TOut)
{
// line below doesn't work
return new Frame<TOut>(Samples.Select(s => (TOut)s));
}
}
「或什麼」真的不能用Google搜索,可以嗎?研究實際的異常消息,你會發現在你的方法定義中缺少一個泛型類型參數:'AsType <'**'TOut' **'>()'。 – CodeCaster