func

converting a .net Func<T> to a .net Expression<Func<T>>

自闭症网瘾萝莉.ら 提交于 2019-11-26 10:20:57
Going from a lambda to an Expression is easy using a method call... public void GimmeExpression(Expression<Func<T>> expression) { ((MemberExpression)expression.Body).Member.Name; // "DoStuff" } public void SomewhereElse() { GimmeExpression(() => thing.DoStuff()); } But I would like to turn the Func in to an expression, only in rare cases... public void ContainTheDanger(Func<T> dangerousCall) { try { dangerousCall(); } catch (Exception e) { // This next line does not work... Expression<Func<T>> DangerousExpression = dangerousCall; var nameOfDanger = ((MemberExpression)dangerousCall.Body).Member

Does Ninject support Func (auto generated factory)?

允我心安 提交于 2019-11-26 05:29:55
问题 Autofac automatically generates factories for Func<T> ; I can even pass parameters. public class MyClass { public MyClass(Func<A> a, Func<int, B> b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If not, what workaround can I apply? Thanks. Update : Just found this post, seems the answer is no: How do I handle classes with static methods with Ninject? 回答1: NB Ninject 3.0 and later has this fully supported using the Ninject.Extensions.Factory package, see the wiki:- https:/

Creating delegates manually vs using Action/Func delegates

隐身守侯 提交于 2019-11-26 05:27:36
问题 Today I was thinking about declaring this: private delegate double ChangeListAction(string param1, int number); but why not use this: private Func<string, int, double> ChangeListAction; or if ChangeListAction would have no return value I could use: private Action<string,int> ChangeListAction; so where is the advantage in declaring a delegate with the delegate keyword? Is it because of .NET 1.1, and with .NET 2.0 came Action<T> and with .NET 3.5 came Func<T> ? 回答1: The advantage is clarity. By

Why Func<T,bool> instead of Predicate<T>?

我是研究僧i 提交于 2019-11-26 04:58:53
问题 This is just a curiosity question I was wondering if anyone had a good answer to: In the .NET Framework Class Library we have for example these two methods: public static IQueryable<TSource> Where<TSource>( this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate ) public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate ) Why do they use Func<TSource, bool> instead of Predicate<TSource> ? Seems like the Predicate

Func<T> with out parameter

送分小仙女□ 提交于 2019-11-26 04:21:02
问题 Can I pass a method with an out parameter as a Func? public IList<Foo> FindForBar(string bar, out int count) { } // somewhere else public IList<T> Find(Func<string, int, List<T>> listFunction) { } Func needs a type so out won\'t compile there, and calling listFunction requires an int and won\'t allow an out in. Is there a way to do this? 回答1: ref and out are not part of the type parameter definition so you can't use the built-in Func delegate to pass ref and out arguments. Of course, you can

converting a .net Func<T> to a .net Expression<Func<T>>

╄→гoц情女王★ 提交于 2019-11-26 02:07:35
问题 Going from a lambda to an Expression is easy using a method call... public void GimmeExpression(Expression<Func<T>> expression) { ((MemberExpression)expression.Body).Member.Name; // \"DoStuff\" } public void SomewhereElse() { GimmeExpression(() => thing.DoStuff()); } But I would like to turn the Func in to an expression, only in rare cases... public void ContainTheDanger(Func<T> dangerousCall) { try { dangerousCall(); } catch (Exception e) { // This next line does not work... Expression<Func