有什么方法可以得到 java List<?>在 C# 中?
我需要一个 IEnumerable<T> , 其中T可以是 class (字符串),或 struct (整数,双...)。
public interface I
{
IEnumerable<object> Enumers { get; }
}
public class A<T> : I
{
IEnumerable<T> ts;
public IEnumerable<object> Enumers
{
get { return (IEnumerable<object>)this.ts; }
}
}
public class Test
{
public static void Main()
{
A<double> a = new A<double>();
var x = a.Enumers; //It crashes here.
}
}
它在运行时崩溃,因为无法从 IEnumerable<T> 进行转换至 IEnumerable<object> .
有什么想法吗?
请您参考如下方法:
您需要获得一个不同的 IEnumerable,其元素已按如下所示进行转换:
IEnumerable<object> Enumers
{
get { return this.ts.Cast<object>(); }
}
在 C# 中有一个叫做 boxing 的东西我猜这是如何将 T 作为结构的,尽管我没有检查。
