【发布时间】:2016-08-23 08:08:03
【问题描述】:
我有一个通用参数 T,它在一个特定情况下是一个数组。是否可以将对象数组转换为typeof(T).GetElementType() 的数组?例如:
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 方法是接口的实现,我无法更改其签名或添加新签名。
-
查看我的回答的修改
标签: c# arrays generics casting