func

How to define a delegate using Delegate.CreateDelegate instead of Func<>?

时光总嘲笑我的痴心妄想 提交于 2019-12-11 03:25:37
问题 I have a method and two delegate like below. It is running in this way. But I want to use Delegate.CreateInstance. The types of the dx and the dy must be Func<IEnumerable<Foo>> . Like below the fx and fy. They must not be Func<int, IEnumerable<Foo>> . public class Test { private IEnumerable<T> CreateItems<T>(int count) where T : class { for (int i = 0; i < count; i++) { yield return (T)Activator.CreateInstance(typeof(T), i.ToString()); } } public List<T> TestMethod<T>(int i = 1) where T :

Stop Ninject from binding Func<T, T, bool> automatically

坚强是说给别人听的谎言 提交于 2019-12-11 03:24:35
问题 I have a PriorityQueue that takes a Func as construction parameter. public PriorityQueue(ISomeOtherInjectedThing other, Func<T, T, bool> cmp_func) {...} I bound this using Ninject: Bind(typeof (IPriorityQueue<,>)).To(typeof(PriorityQueue<,>)); We had a weird bug in code and as part of this, we noticed that Ninject seems to generate an object Func and injects this into our priority queue, but we don't have a binding for this. The factory should have thrown an activtion exception, since we didn

How to map Expression<Func<TEntity, bool>> to Expression<Func<TDbEntity, bool>>

怎甘沉沦 提交于 2019-12-10 13:37:27
问题 How can I map from: Expression<Func<TEntity, bool>> to: Expression<Func<TDbEntity, bool>> where TEntity: class, new() and TDbEntity: class, new() TEntity is from Domain and TDbEntity is from Infrastructure layer, but have same properties. It is possible? 回答1: For relatively simple cases (and I guess in your case expressions are simple) you can use ExpressionVisitor with only a couple of overrides. For example: public static class ExpressionExtensions { public static Expression<Func<TTo, bool>

How can I Create a Expression.Property of a child object

巧了我就是萌 提交于 2019-12-10 13:18:19
问题 normally I create an expresion in this way. ParameterExpression pe = Expression.Parameter(typeof(object1), "x"); string Name = "property1"; MemberExpression left = Expression.Property(pe, (object1).GetProperty(Name)); it produces left = x => x.property1 I need to know how can I produce left = x => x.Object2.property1 if Name = "Object2.property1"; and object2 is a child to object1 Thanks in advance 回答1: I don't quite understand what you want. Is it a property chain (say: x.Prop1.Prop2)? var

Better way to Sort a List by any property

笑着哭i 提交于 2019-12-10 10:19:23
问题 My method receives all DataTables parameters to sort table by column clicked. I call this method from controller of each page list. I'm looking for a better way to do this like a generic method for all types: string , int , decimal , double , bool ( nullable or not ). But I can't find it. My current code: public List<T> OrderingList<T>(List<T> list, DataTablesParam model) { var iColumn = model.Order.FirstOrDefault().Column; var property = typeof(T).GetProperty(model.Columns.ToArray()[iColumn]

Check if a func exists in Swift

霸气de小男生 提交于 2019-12-10 09:19:21
问题 I wish to check if a func exists before I call it. For example: if let touch: AnyObject = touches.anyObject() { let location = touch.locationInView(self) touchMoved(Int(location.x), Int(location.y)) } I would like to call touchMoved(Int, Int) if it exists. Is it possible? 回答1: You can use the optional chaining operator: This seems to only work with ObjC protocols that have @optional functions defined. Also seems to require a cast to AnyObject: import Cocoa @objc protocol SomeRandomProtocol {

T of Func<S, T> is inferred from output of lambda expression only when S and T are different?

ε祈祈猫儿з 提交于 2019-12-10 03:19:33
问题 When S and T are different, this works: public static void Fun<S, T>(Func<S, T> func) { } Fun((string s) => true); //compiles, T is inferred from return type. But, public static void Fun<T>(Func<T, T> func) { } Fun(t => true); //can't infer type. In the first example, since T is inferred from return type of lambda expression, can't T in the second example too be inferred? I suppose its doing it, but why is first T not known, when second T of Func<T, T> is known, after all T == T right? Or is

The request message was already sent. Cannot send the same request message multiple times

拜拜、爱过 提交于 2019-12-10 02:56:24
问题 Is there anything wrong with my code here? I keep getting this error: System.InvalidOperationException: The request message was already sent. Cannot send the same request message multiple times. My HttpRequestMessage is inside a Func so I figured I get a brand new request each time I pass in func(). public async Task<HttpResponseMessage> GetAsync(HttpRequestMessage request) { return await RequestAsync(() => request); } public async Task<HttpResponseMessage> RequestAsync(Func

How do I create a generic Expression that has an expression as a parameter

醉酒当歌 提交于 2019-12-09 01:07:18
问题 There is a DisplayNameFor(x=>x.Title) helper in ASP.Net MVC. I want to implement something similar in behavior. I want to have a method that accepts an expression based on User class ( u=>u.Birthdate or u=>u.Name), a operand (Greater, Less, Equal) and a value like DateTime.Now and returns an expression u=>u.Birthdate > DateTime.Now I understand that I'll have to build resulting expression manually from pieces. What i can't wrap my head around is passing in and handling of property expression.

Implication of using dynamic Expression<Func<T,X>> in NHibernate

社会主义新天地 提交于 2019-12-08 20:00:36
Basically in my application, I am using the following code to build my expression depending on the selection on the page. Expression<Func<T, bool>> expr = PredicateBuilder.True<T>(); expr = expr.And(b => b.Speed >= spec.SpeedRangeFrom && b.Speed <= spec.SpeedRangeTo); ... It has the tendency to end up with a long expression with multiple "or" and "and" conditions. Once I have finished building the expression, I passed it on to my repository which looks like the following: var results = Session.Query<T>().Where(expr).ToList(); The problem with that is it took so long to return me the result. I