func

Moq It.IsAnyType not working for Func returning Task with generic type

微笑、不失礼 提交于 2021-02-10 07:53:51
问题 I've tried 3 different ways to setup a mock for a generic interface method. Only one way works, but it is using an explicit type, so won't work generically. I tried using It.IsAnyType, but it doesn't seem to match on the call that is made. Here is the sample code (I expected test case 1 to have "asdf" returned). How can I get the mock to work for any type (not just string?)? using Moq; using System; using System.Threading.Tasks; namespace blah { public class Junk : IGetOrAddable { public

Extracting Func<> from Expression<>

匆匆过客 提交于 2021-02-08 13:14:22
问题 I wanna extract the Func<> from the following Expression : Expression<Func<IQueryable<Entity>, IOrderedQueryable<Entity>>> order = q => q.OrderByDescending(c=>c.FullName); Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderFunc = ? How can I do it? Edit : And how can we convert Func<IQueryable<Entity>, IOrderedQueryable<Entity>> to Expression<Func<IQueryable<Entity>, IOrderedQueryable<Entity>>> ? 回答1: You can use the Compile method to turn any Expresstion<TDelegate> into a TDelegate .

Extracting Func<> from Expression<>

自古美人都是妖i 提交于 2021-02-08 13:12:23
问题 I wanna extract the Func<> from the following Expression : Expression<Func<IQueryable<Entity>, IOrderedQueryable<Entity>>> order = q => q.OrderByDescending(c=>c.FullName); Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderFunc = ? How can I do it? Edit : And how can we convert Func<IQueryable<Entity>, IOrderedQueryable<Entity>> to Expression<Func<IQueryable<Entity>, IOrderedQueryable<Entity>>> ? 回答1: You can use the Compile method to turn any Expresstion<TDelegate> into a TDelegate .

How can I chain methods depending on user input?

我的未来我决定 提交于 2021-02-05 09:36:16
问题 I have a method that I'm using to process images (rotate, filter, resize, etc). It looks like this: public Image ProcessImage(Image image, Func<ImageFactory, ImageFactory> process) { using (var imageFactory = new ImageFactory(preserveExifData: true)) { using (var imageStream = new MemoryStream()) { var loadResult = imageFactory.Load(image); var processResult = process(loadResult); processResult.Save(imageStream); return Image.FromStream(imageStream); } } } Since I'm using Func<> in this way I

diffrence between running query with Func and Expression Func [duplicate]

旧城冷巷雨未停 提交于 2021-02-04 08:24:05
问题 This question already has answers here : Why would you use Expression<Func<T>> rather than Func<T>? (10 answers) What is the difference between IQueryable<T> and IEnumerable<T>? (12 answers) Closed 2 years ago . I have been searching on internet to find out the difference between Func and Expression Func, somehow i got the point,the first one is just a function gets the data and then apply the function in memory but the second,translate it to sql and run it in the database,after i run this

MOQ- Setting up and verifying a generic method with Func argument

邮差的信 提交于 2021-01-28 19:34:21
问题 I have a third party interface which I want to mock its methods. To make my purpose clear consider the following IFoo interface which has a generic method like M2. One of M2 arguments is of type Func. public interface IFoo { bool M1<T>(); bool M2<T>(T arg, Func<T, string> func); } If I set up the M2 method as: var mock = new Mock<IFoo>(); mock.Setup(foo => foo.M2(It.IsAny<It.IsAnyType>(),It.IsAny<Func<It.IsAnyType, string>>())).Returns(true); mock.Object.M2("arg1", s => s); mock.Verify(foo =>

Why can't a member field have a field initializer call a member function?

核能气质少年 提交于 2021-01-27 18:24:22
问题 Inside a MVC controller I attempted to create a field similar to: Func<MyModel, ViewResult> ModelResult=(model) => View("myview.cshtml", model); This results in the compilation error An object reference is required for the non-static field, method, or property 'System.Web.Mvc.Controller.View(string, object)' This code works fine as a method private ViewResult ModelResult(MyModel model) { return View("myview.cshtml", model); } It also works fine if the field is initialized by the constructor

How can I force a throw to be a statement and not an expression (in a lambda expression)?

≡放荡痞女 提交于 2021-01-20 04:20:19
问题 Starting from C# 7.0 the throw keyword can be used both as an expression and as a statement, which is nice. Though, consider these overloads public static void M(Action doIt) { /*use doIt*/ } public static void M(Func<int> doIt) { /*use doIt*/ } When invoking like this M(() => throw new Exception()); or even like this (with a statement lambda) M(() => { throw new Exception(); }); the M(Func<>) overload is selected by the compiler indicating that the throw is here considered as an expression.

How can I force a throw to be a statement and not an expression (in a lambda expression)?

泪湿孤枕 提交于 2021-01-20 04:19:17
问题 Starting from C# 7.0 the throw keyword can be used both as an expression and as a statement, which is nice. Though, consider these overloads public static void M(Action doIt) { /*use doIt*/ } public static void M(Func<int> doIt) { /*use doIt*/ } When invoking like this M(() => throw new Exception()); or even like this (with a statement lambda) M(() => { throw new Exception(); }); the M(Func<>) overload is selected by the compiler indicating that the throw is here considered as an expression.

How can I force a throw to be a statement and not an expression (in a lambda expression)?

拈花ヽ惹草 提交于 2021-01-20 04:15:28
问题 Starting from C# 7.0 the throw keyword can be used both as an expression and as a statement, which is nice. Though, consider these overloads public static void M(Action doIt) { /*use doIt*/ } public static void M(Func<int> doIt) { /*use doIt*/ } When invoking like this M(() => throw new Exception()); or even like this (with a statement lambda) M(() => { throw new Exception(); }); the M(Func<>) overload is selected by the compiler indicating that the throw is here considered as an expression.