What alternatives can be used to represent .NET's 'from-end' indexer expression inside a lambda/expression tree?

179 views Asked by At

I wrote a mock for a test which tries to mimic a method involving any file uploaded with a ".jpg" extension and decided to use .NET's 'from-end' indexer expression for convenience in checking if a file is indeed a JPEG file through the provided filename. However, the compiler complains with CS8790: An expression tree may not contain a pattern System.Index or System.Range indexer access. What alternatives I could use which achieves the same result/ease of use as the expression itself?

This code doesn't work:

_mock.Setup(f => f.DownloadFileAsync(
    It.IsNotNull<Guid>(),
    It.Is<string>(s => s.Split('.', StringSplitOptions.None)[^1] == "jpg"), // CS8790 Error
    It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(new BinaryData(Array.Empty<byte>())));

The code below works because it uses normal indexing:

_mock.Setup(f => f.DownloadFileAsync(
    It.IsNotNull<Guid>(),
    It.Is<string>(s => s.Split('.', StringSplitOptions.None)[1] == "jpg"), // compiles fine.
    It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(new BinaryData(Array.Empty<byte>())));
0

There are 0 answers