Initialize List<T> with array in CLR C++?

倖福魔咒の 提交于 2019-12-22 06:40:10

问题


I was wondering why this code does not work in C++/CLI but damn easy in C#?

List<Process^>^ processList = gcnew List<Process^>(
  Process::GetProcessesByName(this->processName)););

error C2664: 'System::Collections::Generic::List::List(System::Collections::Generic::IEnumerable ^)' : cannot convert parameter 1 from 'cli::array ^' to 'System::Collections::Generic::IEnumerable ^'

Here is what I come up with. Did perfectly well. :)

List<Process^>^ processList = gcnew List<Process^>(
  safe_cast<System::Collections::Generic::IEnumerable<Process^>^>
    (Process::GetProcessesByName(this->processName)));

回答1:


You need to use safe_cast. According to the MSDN documentation on System::Array,

Important

Starting with the .NET Framework 2.0, the Array class implements the System.Collections.Generic::IList<T>, System.Collections.Generic::ICollection<T>, and System.Collections.Generic::IEnumerable<T> generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

As you can see, the cast must be done explicitly in C++ at runtime, e.g.

List<Process^>^ processList = gcnew List<Process^>(
    safe_cast<IEnumerable<T> ^>(
        Process::GetProcessesByName(this->processName)));


来源:https://stackoverflow.com/questions/12152913/initialize-listt-with-array-in-clr-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!