如何使用C#中的Cast修复投放错误?

时间:2019-03-27 14:29:09

标签: c# casting

SystemIO[] s = Enum.GetNames(typeof(SystemIO)).Cast<SystemIO>().ToArray();

我想将string[]投射到SystemIO[]

我收到以下错误:

  

跟踪:位于System.Linq.Enumerable.d__97`1.MoveNext()
  在System.Linq.Buffer`1..ctor(IEnumerable`1源)处
  在System.Linq.Enumerable.ToArray [TSource](IEnumerable`1源)
  在TestCase.TestCaseGeneral.AllIos()

为什么我不能这样投射?

1 个答案:

答案 0 :(得分:3)

如果要获取,请放std::span

#include <span>
// or:
// #include <gsl/span>

int main() {

    // ... etc. ...

    {
        size_t size = 10e5;
        auto uptr { std::make_unique<double[]>(size) };
        do_stuff_with_the_doubles(std::span<int> my_span { uptr.get(), size });
    }

    // ... etc. ...
}

如果要名称更改Enum.GetValues类型(名称为SystemIO[] s = Enum .GetValues(typeof(SystemIO)) .Cast<SystemIO>() .ToArray(); s):

s