func

Check if a func exists in Swift

我只是一个虾纸丫 提交于 2019-12-05 15:57:56
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? 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 { @optional func aRandomFunction() -> String @optional func anotherRandomFunction() -> String } class

Reflection MemberInfo to Func<T1, T2>

 ̄綄美尐妖づ 提交于 2019-12-05 08:43:43
I'm looking for a method to convert instance of MemberInfo to "Func" type (to use it through lambda expression later). Lets, say I have a member function of type public bool func(int); Using reflection, I somehow get instance of MemberInfo "mi", now i want to convert it to Func<int, bool>; type. something like: Func<int, bool f = myType.GetMember(mi.Name); Is there a way to do it? ps. Marc Grawell's answer resolves my issue, no need for further comments Func<int,bool> f = Delegate.CreateDelegate( typeof(Func<int,bool>), target, (MethodInfo)mi); Note here that target is the object you want to

Func<> getting the parameter info

佐手、 提交于 2019-12-05 05:17:44
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 NotImplementedException(); } I want to get the value of query inside the Filter extension method assigned to _target If you

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

こ雲淡風輕ζ 提交于 2019-12-05 04:01:16
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<HttpRequestMessage> func) { var response = await ProcessRequestAsync(func); if (response.StatusCode == HttpStatusCode

Get the name of a field from a class without an instance

爷,独闯天下 提交于 2019-12-05 03:27:54
So I use the following utility to get the name of a field/property from an instance of a class... public static string FieldName<T>(Expression<Func<T>> Source) { return ((MemberExpression)Source.Body).Member.Name; } This allows me to do the following: public class CoolCat { public string KaratePower; } public class Program { public static Main() { public CoolCat Jimmy = new CoolCat(); string JimmysKaratePowerField = FieldName(() => Jimmy.KaratePower); } } This is great for serialization and other times when I need a string representation of the field name. But now, I want to be able to get the

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

守給你的承諾、 提交于 2019-12-05 00:32:19
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 byte } To handover the method I want to assign them to a Func delegate. But I ran into the problem that

How to invoke Expression<Func<Entity, bool>> against a collection

て烟熏妆下的殇ゞ 提交于 2019-12-05 00:17:33
I have an interface that defines a repository from the Repository pattern: interface IRepository { List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression); } I've implemented it against Entity Framework: class EntityFrameworkRepository { public List<Customer> GetAllCustomers(Expression<Func<Customer, bool>> expression) { return DBContext.Customers.Where(expression).ToList(); } } That seems to work well, it allows me to do something like: var customers = entityFrameworkRepository.Where( customer => String.IsNullOrEmpty(customer.PhoneNumber) ); Now I'd like to have an

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

不想你离开。 提交于 2019-12-04 23:45:41
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, string>> and need to convert it to a Func<T, bool> that returns true/false based on if the returned

A List<> of Func<>s, compile error with generic return type, but why?

霸气de小男生 提交于 2019-12-04 22:56:32
This is a bit of a lengthy question, so please bear with me. I need to create a mapping between a set of strings and corresponding generic method calls for each string. However I've run into a compile issue, explained lower down. In my scenario I am using a Dictionary<> , but the issue exists equally for a List<> . For simplicity I'm using a List<> in the example below. Consider these three classes: public abstract class MyBase { /* body omitted */ } public class MyDerived1 : MyBase { /* body omitted */ } public class MyDerived2 : MyBase { /* body omitted */ } And a method in some other class:

Generic expression abstraction issue

纵然是瞬间 提交于 2019-12-04 17:25:26
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 this method a bit further by providing SetOwnedCollectionMapping() . This means the calling code can