Azure / azure-storage-net

Microsoft Azure Storage Libraries for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot convert type 'System.Collections.Generic.List<Microsoft.Azure.Cosmos.Table.ITableEntity>' to 'System.Collections.Generic.List<ITableEntity>'

KyleRosenberg opened this issue · comments

commented

Which service(blob, file, queue, table) does this issue concern?

Table

Which version of the SDK was used?

1.0.7

Which platform are you using? (ex: .NET Core 2.1)

.NET Core 3.1

What problem was encountered?

I am trying to mock CloudTable storage for unit tests. I am trying to override ExecuteQuerySegmentedAsync with this signature

public virtual Task<TableQuerySegment<TElement>> ExecuteQuerySegmentedAsync<TElement>(TableQuery<TElement> query, TableContinuationToken token) where TElement : ITableEntity, new();

I also used the reflection code from #619 to generate an instance of TableQuerySegment, but I do not believe that is related to the issue.

Specifically I am seeing the error on the line
List<ITableEntity> results = (List<ITableEntity>)rows.FindAll(x => true);

How can we reproduce the problem in the simplest way?

Here is the relevant code I have tried

using Microsoft.Azure.Cosmos.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace MockStorage
{
    public class MockCloudTable : CloudTable
    {
        private List<ITableEntity> rows;
        public async override Task<TableQuerySegment<ITableEntity>> ExecuteQuerySegmentedAsync<ITableEntity>(TableQuery<ITableEntity> query, TableContinuationToken token)
        {
            List<ITableEntity> results = (List<ITableEntity>)rows.FindAll(x => true);
            var ctor = typeof(TableQuerySegment<ITableEntity>)
                .GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
                .FirstOrDefault(c => c.GetParameters().Count() == 1);

            TableQuerySegment<ITableEntity> mock = ctor.Invoke(new object[] { results }) as TableQuerySegment<ITableEntity>;

            return await Task.Run(() => mock);
        }
    }
}

Have you found a mitigation/solution?

If I use a for loop instead of the FindAll linq expression, it works.

commented

Found a workaround, if I use a for loop instead of the FindAll linq expression, it works.