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

How do I declare a Func Delegate which returns a Func Delegate of the same type?

隐身守侯 提交于 2020-01-02 01:00:10
问题 I'd like to write a method which does some work and finally returns another method with the same signature as the original method. The idea is to handle a stream of bytes depending on the previous byte value sequentially without going into a recursion. By calling it like this: MyDelegate executeMethod = handleFirstByte //What form should be MyDelegate? foreach (Byte myByte in Bytes) { executeMethod = executeMethod(myByte); //does stuff on byte and returns the method to handle the following

Convert Func<T, String> to Func<T, bool>

断了今生、忘了曾经 提交于 2020-01-02 00:54:08
问题 I think my mind is exploding trying to figure out Funcs... If this makes no sense, I apologize, right now it make sense to me but its been a long day already .... 1) Assuming you are given a func which takes in T and outputs a string: Func<T, string> Can you transform that into a func that take in a T and returns a bool based on some logic (in this case if the returned string is empty (String.IsNullOrWhiteSpace)? Func<T, bool> 2) Can you do the same thing if you are given an Expression<Func<T