func

Calling a method as a method parameter but not executing?

孤街醉人 提交于 2020-05-09 10:28:10
问题 Hi I am trying to implement a method that will take a method (any method in the grand scheme of things) as a parameter. I want this parameter method to run when in the method that called it only, If the method that passes into this method has a return value then it should still be able to return its value. I want to measure the performance of the methods that are passed in. return Performance(GetNextPage(eEvent, false)); public static T Performance<T>(T method) { T toReturn; Stopwatch sw =

How to pass a delegate with different number of arguments than originally designed

佐手、 提交于 2020-04-18 07:11:09
问题 I have the following issue: I have designed a framework that uses a generic delegate function (Func<...>) to calculate distance between two real number vectors The delegate has the following form: Func<double[], double[], double> distance; I have many different distance functions implemented I would rather not change the signature of distance delegate I need to create a new function that besides two double vectors needs some additional parameters i.e. Func<double[], double[], double, double,

How to use go template to parse html files with FuncMap

一世执手 提交于 2020-01-24 12:01:27
问题 I use following code to parse html template. It works well. func test(w http.ResponseWriter, req *http.Request) { data := struct {A int B int }{A: 2, B: 3} t := template.New("test.html").Funcs(template.FuncMap{"add": add}) t, err := t.ParseFiles("test.html") if err!=nil{ log.Println(err) } t.Execute(w, data) } func add(a, b int) int { return a + b } and html template test.html. <html> <head> <title></title> </head> <body> <input type="text" value="{{add .A .B}}"> </body> </html> But when I

How to get Method Name of Generic Func<T> passed into Method

寵の児 提交于 2020-01-14 07:55:12
问题 I'm trying to get the method name of a function passed into an object using a .Net closure like this: Method Signature public IEnumerable<T> GetData<T>(Func<IEnumerable<T>> WebServiceCallback) where T : class { // either gets me '<LoadData>b__3' var a = nrdsWebServiceCallback.Method.Name; var b = nrdsWebServiceCallback.GetInvocationList(); return WebServiceCallback(); } I'm calling it like this: SessionStateService.Labs = CacheManager.GetData(() => WCFService.GetLabs(SessionStateService.var1,

Generic expression abstraction issue

隐身守侯 提交于 2020-01-13 06:09:11
问题 I have the following method SetMapping() which is used to define some mapping setting using expressions. public class AggregateMap<TDataEntity> { protected Expression<Func<IUpdateConfiguration<TDataEntity>, object>> graphMapping; protected void SetMapping(Expression<Func<IUpdateConfiguration<TDataEntity>, object>> mapping) { graphMapping = mapping; } } Example calling code: SetMapping(map => map.OwnedCollection(root => root.ChildEntities)); The above works great, but I would like to abstract

Passing a Func<int, int> to Task.Run() in C# without using lambda expressions

天涯浪子 提交于 2020-01-06 08:04:09
问题 I try to get my head around the Task/Func/Action/await functionality of C# but still need your help: I have a button click event handler in my gui thread that, when called, does the following: Func<int> t = new Func<int>(CountToBillion); int result = await Task.Run(t); //do something with result value... The method itself is declared as: private int CountToBillion() { //count to 1 billion and return 0 } So far, this runs without error. But if I want to pass a parameter to CountToBillion()

The #selector is not compatible with the closure?

不打扰是莪最后的温柔 提交于 2020-01-04 03:01:06
问题 I have tried to implement the custom function with the closure. But it's does not supported by #selector . Here's an example: class Core: NSObject { static let shared:Core = Core.init() func button(viewController: UIViewController, button: UIButton, title: String, color: UIColor, completion: () -> Void) { button.layer.cornerRadius = button.bounds.width / 2 button.setTitle(title, for: .normal) button.setTitleColor(UIColor.white, for: .normal) button.backgroundColor = color button.titleLabel?

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

♀尐吖头ヾ 提交于 2020-01-03 02:56:14
问题 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 =

How to create a Func<> delegate programmatically

孤者浪人 提交于 2020-01-02 08:07:09
问题 I have a small dependency injection framework, and I am trying to make it resolve Lazy<> instances dynamically. The idea is to do something like that: DIContainer.Register<IDbCommand,SqlCommand>(); var lazyCommand = DIContainer.Resolve<Lazy<IDbCommand>>(); I read the other day that Autofac was able of doing that. I am stuck trying to set the constructor for that Lazy<> instance. In the next test code, a exception is thrown because the desired type constructor is expecting a Func<arg> , but I

Func<> getting the parameter info

橙三吉。 提交于 2020-01-02 03:04:06
问题 How to get the value of the passed parameter of the Func<> Lambda in C# IEnumerable<AccountSummary> _data = await accountRepo.GetAsync(); string _query = "1011"; Accounts = _data.Filter(p => p.AccountNumber == _query); and this is my extension method public static ObservableCollection<T> Filter<T>(this IEnumerable<T> collection, Func<T, bool> predicate) { string _target = predicate.Target.ToString(); // i want to get the value of query here.. , i expect "1011" throw new