question

ArathiJinde-8885 avatar image
0 Votes"
ArathiJinde-8885 asked JeanineZhang-MSFT commented

C++ CLI .NET core 5 function returning IEnumerable

Hi
how to return IEnumerable from a C++/CLI .NET Core library in a function
List<MyClass> li = gcnew List<MyClass>();

IEnumerable<MyClass^>^ Find(String^ key)
{
// how to return from list
}

Also Task/await is not supported in C++/CLI core dll what is the alternative to implement in a function

the below implementation is not possible as this is unsupported in C++/CLI what is the alternative to this , please suggest
Task <IEnumerable<MyClass^>> ^ Find(String^ key)
{
return Task.CompletedTask
}

c++dotnet-cli
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered JeanineZhang-MSFT commented

Hi,

If you want to return IEnumerable, I suggest you should try to use FindAll instead of Find. And you'll need to use a delegate explicitly.

For example:

 bool IsAge(MyClass^ item) 
 {
     return item->Age == 12;
 }
    
 IEnumerable<MyClass^>^ result = list->FindAll(gcnew System::Predicate<MyClass^>(IsAge));

Could you please provide more details about the second question? What do you want to achieve?

Best Regards,

Jeanine



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

public interface IRepository<TModel, TKey> : IReadOnlyRepository<TModel, TKey> is in C#

public ref class Wrapper : IRepository<MyClass, string> in C++/CLI
{
Task<IEnumerable<MyClass>^>^ FindAsync(TKey^ key) //interface implementation from C#
{

         return Task.FromResult(this.lst.Where(x = > key.Equals(this.keyAccessor(x))));
         }

}
Task /await is not supported with C++/CLI??
any alternative to this is possible

0 Votes 0 ·

@ArathiJinde-8885

If you want to implement the method as a true asynchronous method, you'll need to use the Task Parallel Library directly. If you want to implement the method synchronously, you can use Task.FromResult to return a completed Task<TResult> instance with the Result property set to the return value from your method.


0 Votes 0 ·