I just found the weirdest behavior in Linq:
When calling unary functions I like to just pass the function name, so instead of
var foo = myList.Select(item => MyFunc(item));
I write
var foo = myList.Select(MyFunc);
which should be the same. Only in some cases, it isn't! Namely if the function has a second parameter which is an int and optional:
private string MyFunc(string input, int foo = 0)
{
...
}
In this case, the statement
var foo = myList.Select(MyFunc);
equals
var foo = myList.Select((item, index) => MyFunc(item, index));
If the second parameter is either not opional or not an int, the compiler complains, but in this case, it just sneakily surprises you.
Has anyone else encountered this? Which other Linq expressions work this way? (so far, .SelectMany() does). And what is the most elegant way to work around this behavior (and keep others from falling into the same trap?)
This is not really an issue of specific LINQ extension method, but how optional parameters are handled for Funcs and Actions, in short - they are not, they are considered a regular parameter and default value is omitted when selecting a corresponding Func/Action signature. Take a look here Optional Parameters, No overload for 'Employee' matches delegate 'System.Func<Employee> or here Invoke Func<T1, T2, T3> which has optional parameters?.
In other words, your MyFunc cannot be used as Func<string, string>, you must use Func<string, int, string>, which in case of Select happens to be present as an overload with index added.
Related
Is it possible in C# to have higher-order function which takes a function with an arbitrary number and type of arguments and return values, e.g.,
// No valid C# code
void MyFunction(Func f)
{
// do something
}
MyFunction(string.Join); // type: Func<String, IEnumerable<String>, String>
MyFunction(string.CompareTo); // type: Func<object, int>
// ...
I guess, this question really boils down to whether there's a most generic type for functions which can hold any function with possibly different number and type of arguments.
My use-case is the following: I have a method which takes a MethodInfo object. Currently, I do something like
DoSomething(((Func<object, int>)string.CompareTo).Method)
However, I'd have to repeat this for every different kind of function I expect. I'd prefer a solution in which DoSomething could by itself determine the type, so I just give it the Function itself instead of the MethodInfo object.
EDIT:
To be more precise, I'm writing a Linq-To-X provider using ReLinq. I got some example code from a tutorial blog on this topic (https://www.codeproject.com/Articles/42059/re-linq-ishing-the-Pain-Using-re-linq-to-Implement and https://weblogs.asp.net/dixin/understanding-linq-to-sql-10-implementing-linq-to-sql-provider). For eager execution functions like Sum or Count, I need to wrap them into a MethodCallExpression object.
In the example code, it is solved like this:
public static IQuery CountToSql<T>(this IQueryable<T> source)
{
ArgumentChecker.CheckNull(source, nameof(source));
var expression =
Expression.Call(
null,
GetMethodInfo(Queryable.Count, source),
new Expression[] { source.Expression }
);
return ToQuery(expression, ((DbQueryable<T>)source).SyntaxProvider);
}
private static MethodInfo GetMethodInfo<T1, T2>(Func<T1, T2> f, T1 unused1 => f.Method;
private static MethodInfo GetMethodInfo<T1, T2, T3>(Func<T1, T2, T3> f, T1 unused1, T2 unused2) => f.Method;
// ...
However, I don't want to write this for every type of function (e.g., Queryable.Sum, Queryable.Count, ...). The current solution necessitates it. I just want one single method that can handle all, as they look the same for 90%.
It seem strange that you need to call such different delegates from one place.
I think you need something like "EventDispatcher", when your code got data that should be then casted to object, but after that inside a call you want to operate a real objects.
If I wrong please tell more about task.
If I right, you have to replace reflections by generics. Create method that will wrap you delegate by Action.
public Action<object> WrapCall<T, K>( Func<T, K> func)
{
return (data) => func((T)data);
}
public Action<object, object> WrapCall<T1, T2, K>(Func<T1, T2,K> func)
{
return (t1Data, t2Data) => func((T1)t1Data, (T2)t2Data);
}
and so one.
It may be not solve your problem but will helps you to find solution in other way.
I'm messing around with expression trees, but I'm little stuck.
I have this expression:
Expression<Func<IX, int>> expr = i => i.GetAll(1, b, method());
Where :
int b = 2;
public static int method()
{
return 3;
}
public interface IX
{
int GetAll(int a, int b, int c);
}
Now I want to get name of the method and values of parameters for this method. Name of the method is easy, but parameter values are harder part. I know I can parse them myself, but I would need to handle all cases (ConstantExpression, MemberExpression, MethodCallExpression and maybe more I'm not aware of). So I was thinking if there was "general" way to get their values. eg 1, 2, 3.
You can get the arguments of the MethodCallExpression in question
and create compiled Func<object>s from them (boxing value-types if necessary), which can then be evaluated.
E.g.:
var args = from arg in ((MethodCallExpression)expr.Body).Arguments
let argAsObj = Expression.Convert(arg, typeof(object))
select Expression.Lambda<Func<object>>(argAsObj, null)
.Compile()();
This will obviously blow up if the expression's body is not a method-call expression or if any of the arguments to the method cannot be evaluated as is (e.g. if they depend on the argument to the expression).
Obviously, you can do a better job if you know the types of the arguments to the method beforehand. For your specific example, this should work:
var args = from arg in ((MethodCallExpression)expr.Body).Arguments
select Expression.Lambda<Func<int>>(arg, null)
.Compile()();
Can you not just get all the parameters and then compile and execute them? I don't see how it would be possible to get all the values without executing if they can be method calls.
I'm a bit fuzzy on the new Action/Func/Variance/CoVariance stuff, which is probably what I need.
What I want is to be able to pass a delegate as a parameter to a method, that takes a string and returns a bool. The problem is that I can't use a typed delegate or interface since it will be used in different libraries which doesn't share libraries, and both will be invoked by a third.
So in general I want the delegate to be inferred by it's input and returning type.
So it'll be like:
delegate bool IncludeItemDelegate(string item);
ClassA.FetchItems(int someParameter,includeItemDelegate);
ClassB.FetchItems(int someParameter,string someOtherParam,includeItemDelegate);
Where A and B doesnt share any libraries, can it be done?
How about Func<string,bool> ?
Predicate is built-in and also signals intent:
ClassA.FetchItems(int someParameter, Predicate<string> filter);
It's also possible to pass the Predicate as a lambda
class A
{
static public IEnumerable<string> FetchItems(int max, Predicate<string> filter)
{
var l = new List<string>() {"test", "fest", "pest", "häst"};
return l.FindAll(filter).Take(max);
}
}
like this
var res = A.FetchItems(2, s => s.EndsWith("est"));
I'm trying to get the name of a method on a type using a lambda expression. I'm using Windows Identity Foundation and need to define access policies with the type name with namespace as a resource and the method name as the action. Here is an example.
This is the type I would be getting the type name and method name from:
namespace My.OrderEntry {
public class Order {
public void AddItem(string itemNumber, int quantity) {}
}
}
This is how I would like to define the access policy through a DSL:
ForResource<Order>().Performing(o => o.AddItem).AllowUsersHaving(new Claim());
From that statement, I would like to get "My.OrderEntry.Order" as the resource and "AddItem" as the action. Getting the type name with namespace is no problem, but I don't think I can use a lambda for a method like I'm trying to do.
public static IPermissionExp Performing<T>(
this IActionExp<T> exp,
Func<T, delegate???> action) {} //this is where I don't know what to define
Is this sort of thing even possible to do? Is there another way to do this sort of thing without using magic strings?
There are two ways to do this:
1: You could make overloads that take the various Func and Action delegates(eg Expression<Func<T, Func<TParam1,TParam2, TReturn>>. Note that your callers would need to specify the generic parameters explicitly, either in the method call or by creating the delegate. This would be used like this:
ForResource<Order>().Performing(o => new Action<string>(o.AddItem)).AllowUsersHaving(new Claim());
2: You could take an Expression<Action> that contains a method call, and parse out the MethodInfo being called from the expression tree. This would be used like this:
ForResource<Order>().Performing(o => { o.AddItem(null); }).AllowUsersHaving(new Claim());
It looks like this is what you are looking for if you want the name of the action delegate method passed in to the Performing function.
public static IPermissionExp Performing<T>(
this IActionExp<T> exp,
Expression<Action<T, string, int>> action)
{
var expression = action.Body as MethodCallExpression;
string actionMethodName = string.Empty;
if (expression != null)
{
actionMethodName = expression.Method.Name;
}
// use actionMethodName ("AddItem" in the case below) here
}
This would allow you to call the method like this...
ForResource<Order>().Performing((o, a, b) => o.AddItem(a, b)).AllowUsersHaving(new Claim());
I recently did a thing at work where you defined the a method using a lambda, which the internal object then took the name of. You could use strings as well, or pass in a MethodInfo but the first one isn't really type safe (and typos are a big risk), and the latter is not very elegant.
Basically I had a method like this (this is not the exact method, it is a bit more advanced):
public void SetRequest(Request req, Expression<Func<Service, Func<long, IEnumerable<Stuff>>> methodSelector);
The key here is the "Expression" thing, this lets you "select" a method like this:
SetRequest(req, service => service.SomeMethodTakingLongReturningStuffs);
Method selector is made into a expression tree which you can then fetch different bits of data from. I don't recall exactly what the resulting tree looks like, it also depends on how your lambdas look.
You could pass it in as a Action instead, which doesn't force any return type. It is still a little messy though, because you have to pass some arguments to the method in order for it to compile.
1) What is the real definition for Action delegate? some definitions describe it is as polymorphic conditional map , some say it *Applied decision Table *.
(You may ask what will you achieve by knowing definition , if i know it i can understand its real purpose).
2) Thanks Binary Worrier,Andrew Hare of stackoverflow for giving nice examples.
When i declare
string[] words = "This is as easy as it looks".Split(' ');
`Array.ForEach(words, p => Console.WriteLine(p));`
i can understand what it actually does.But when i declare ,How does C# interpret when i
declare
Dictionary<SomeEnum, Action<User>> methodList =
new Dictionary<SomeEnum, Action<User>>()
methodList.Add(SomeEnum.One, DoSomething);
methodList.Add(SomeEnum.Two, DoSomethingElse);
Does it store collections of Actions in dictionary ?.unfortunately as the example was incomplete i did not get it.
3) What is the functional difference between Action , Function ,Predicate delagets?
It's just another delegate. Action<T> is declared like this:
void Action<T>(T item)
It's just "something which acts on a single item". There are generic overloads with more type parameters and normal parameters. In itself, an Action<T> isn't an applied decision table or anything like that - it's just a delegate which can do "something" with an item.
The dictionary example is just a dictionary with enum values as keys, and actions as values - so you can look up what to do based on the enum value, and then pass in a User reference for it to act on.
As for Func vs Action vs Predicate: Func is like Action, but returning a value. Predicate is similar, but always returns bool, and there aren't the range of generic overloads, just Predicate<T> to determine if an item "matches" the predicate.
Action, Func and Predicate have different signatures:
void Action<...>(...)
T Func<..., T>(...)
bool Predicate<T>(T)
Action<...> is the same as Func<..., void>
Predicate<T> is the same as Func<T, bool>
1) the Action delegates
(Action, Action<T>, Action<T, T2> ...)
are general purpose delegate to avoid the creation of to many delegate in your application. The idea is :
//- Action => void method with 0 args
//- Action<T> => void method with 1 arg of type T
//- Action<T, T2> => void method with 2 args of type T et T2
//...
2) that dictionary stores for each 'SomeEnum' values, a method wicth match this signature :
void MethodExample (User arg);
Here is an example :
public Init() {
deleteUserMethodsByStatus = new Dictionary<SomeEnum, Action<User>>();
deleteUserMethodsByStatus.Add(UserStatus.Active, user => { throw new BusinessException("Cannot delete an active user."); });
deleteUserMethodsByStatus.Add(UserStatus.InActive, DoUserDeletion});
}
//This is how you could use this dictionary
public void DeleteUser(int userId) {
User u = DaoFactory.User.GetById(userId);
deleteUserMethodsByStatus[u.Status](u);
}
//the actual deletion process
protected internal DoUserDeletion(User u) {
DaoFactory.User.Delete(u);
}
3) Difference between Action , Function ,Predicate :
- an action is a void method(no return value)
- a function is a non void method (has a return value)
- a predicate must return a boolean value and take 1 argument (it basically answere yes or no to question that take 1 argument)
I hope this help.