我有一个泛型参数T,它是一个特定情况下的数组。是否可以将对象数组投射到typeof(T).GetElementType()
的数组?例如:C#将数组强制转换为元素类型
public TResult Execute<TResult>()// MyClass[] in this particular case
{
var myArray = new List<object>() { ... }; //actual type of those objects is MyClass
Type entityType = typeof(TResult).GetElementType(); //MyClass
//casting to myArray to array of entityType
TResult result = ...;
return result;
}
感谢您的回复,但问题在于Execute方法是接口的实现,我无法更改其签名或添加新的签名。 –
查看编辑我的答案 – InBetween