Generic parameter delegate? - c#

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"));

Related

Explanation on this code with delegates

I read quite some articles about delegates, and yes, at first the syntax is confusing. I found this article the most useful. Example 2 makes it quite understandable how to use delegates. But I have this code given to me and have work with it:
public delegate bool IntPredicate(int x);
public delegate void IntAction(int x);
class IntList : List<int>
{
public IntList(params int[] elements) : base(elements)
{
}
public void Act(IntAction f)
{
foreach (int i in this)
{
f(i);
}
}
public IntList Filter(IntPredicate p)
{
IntList res = new IntList();
foreach (int i in this)
if (p(i))
res.Add(i);
return res;
}
}
Now, what confuses me here is the f and p variables in the Act and Filter functions. As in the tutorial, those functions seem to be normal, with normal type of their attributes, but here the attributes are of the delegate functions type and I get confusled.
Can you please enlighten me a bit on this matter?
A delegate is just a type. With the types you're used to (like int, string etc.), when you want to use them, you either use one that is in the framework or you declare your own. You can do exactly the same with delegates - either use a prebuilt one (like System.Action) or declare your own, which is what was done here.
So, in your code snippet, 3 types are declared:
public delegate bool IntPredicate(int x);
public delegate void IntAction(int x);
class IntList : List<int> { ... }
You'll notice that the delegate declarations are on the same level as the class declaration.
When you have a type (like your IntPredicate here), you can then use it for variables or function parameters. The questions now are: how do you set the value of the variable, and what do you do with it then?
With ordinary variables, you just pass in the value. Like this:
string text = "Hello world";
The principle is the same with delegates, but, of course, you have to pass in something that is of the delegate type or something that can be converted to it. You have several options:
Existing method
You can pass in a method, if its signature (that is, the return value and parameters) match those of the delegate. So, you could do this:
void WriteIntAction(int value)
{
Console.WriteLine(value);
}
/* then, in some other method */
IntList intList = new IntList(1,2,3);
intList.Act(WriteIntAction);
Anonymous method
There are several ways to create an anonymous method. I'm going to go with lambda expression, because that is simplest. If you've ever worked with any functional languages, this should be familiar.
IntList intList = new IntList(1,2,3);
intList.Act(x => Console.WriteLine(x));
So, after you have your variable set up with the method you need (whether existing or anonymous), you can simply use the delegate variable as you would any method. This is what this line does:
f(i);
Just be aware that delegate is a reference type, so the value of f here can be null, which will then throw an exception when you try to call a delegate.
TL;DR
A delegate is a type. You can use it in a variable or method parameter. You can pass a method in just using its name or you can create an anonymous method. You can then call the method you passed it by using the variable as you would a method.
You can read more online, for example here: http://msdn.microsoft.com/en-us/library/ms173171.aspx
A delegate type is, for all intents and purposes, just a function (or if you are a C++ user, akin to a function-pointer). In other words, you call them just as if they were a function, which is exactly what the sample code does.
f(i) calls the passed function with the i variable as its sole argument, just as it looks.

I need a Linq IEnumerable<>.Cast(typeof(T))

I'm querying a data structure and the result type is IEnumerable<IEntry> (IEntry being a framework interface), each entry has a Data property (of type object) which is interseting for me.
My code looks like this:
var resultList = framework.QueryAllOfType(queryClause.Type)
.Select(e => e.Data)
.ToList();
deleagte.DynamicInvoke(new[]{resultList});
The method behind the delegate looks something like this:
void Foo (IEnumerable<SomeType> bar); // if queryClause.Type == typeof(SomeType)
void Foo (IEnumerable<OtherType> bar); // if queryClause.Type == typeof(OtherType)
I'm absolutely positive that queryClause.Type matches SomeType, of course however, the .NET framework is not ;-)
Unfortunately this means that the resultList is of type IEnumerable<object> although all the objects within are of the correct type, I'm not able to call the delegate (exception: IEnumerable<object> cannot be converted into IEnumerable<SomeType>).
I know why this is the case, but what's the solution? I would need something along the lines of:
.Select(e => e.Data).Cast(queryClause.Type).ToList() which should return an IEnumerable<queryClause.Type>. Is there such a thing already somewhere in the .NET framework? Is there a better solution?
Important: As two answers already misunderstood my intensions, I cannot use the type as a generic parameter as it is known at runtime only. Therefore all Cast<...>(), Select(e =e as ...), etc. do not work.
You can invoke Cast using reflection:
var method = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(queryClause.Type);
IEnumerable<object> cSeq = (IEnumerable<object>)method.Invoke(null, new object[] { resultList });
deleagte.DynamicInvoke(new[]{ cSeq });
Base on your feedback and using the other answers I've created a ToListOfType() extension method that should do what you need.
public static class EnumerableExtensions
{
private static readonly Type _enumerableType = typeof(Enumerable);
public static IEnumerable CastAsType(this IEnumerable source, Type targetType)
{
var castMethod = _enumerableType.GetMethod("Cast").MakeGenericMethod(targetType);
return (IEnumerable)castMethod.Invoke(null, new object[] { source });
}
public static IList ToListOfType(this IEnumerable source, Type targetType)
{
var enumerable = CastAsType(source, targetType);
var listMethod = _enumerableType.GetMethod("ToList").MakeGenericMethod(targetType);
return (IList)listMethod.Invoke(null, new object[] { enumerable });
}
}
This should work with your delegate if you simply replace your ToList() call with ToListOfType(queryClause.Type). I implemented both the CastAsType and ToListOfType methods so that you can leave the collection un-iterated if you so choose. I'll note for future readers that these are only useful in a situation like yours where you're passing the result off to a delegate dynamically - the Cast<T> operation would be preferred in all other cases.
Try to finish with something like:
.ToList<IEntry>()
is like for example if you do something like:
int[] text = {01, 2, 3, 4, 5};
List<string> list = text.Select(x => x.ToString()).ToList<string>();

Passing IEnumerable data from LINQ as parameter to a method [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I pass an anonymous type to a method?
I have the following LINQ Statement, whose output has to be processed in another method:
var data = from lines in File.ReadAllLines(TrainingDataFile)
.Skip(ContainsHeader ? 1 : 0)
let f = lines.Split(new[] { FieldSeparator }).ToList<String>()
let target = f[TargetVariablePositionZeroBased]
select new { F=f, T=target };
What should be the datatype of the parameter in the method that will take this data?
You can not return the anonymous data types from a method. You can define a class and return object of that class from query and pass it to target method.
public class SomeClass
{
public string F {get; set;}
public string T {get; set;}
}
var data = from lines in File.ReadAllLines(TrainingDataFile)
.Skip(ContainsHeader ? 1 : 0)
let f = lines.Split(new[] { FieldSeparator }).ToList<String>()
let target = f[TargetVariablePositionZeroBased]
select new SomeClass { F=f, T=target };
You can pass the query result IEnumerable<SomeClass> to method as parameter.
public void MethodToCall(IEnumerable<SomeClass> someClass)
{
}
To call the method by passing the query result (IEnumerable<SomeClass>) that is stored in data in this sample code
MethodToCall(data);
You can't very easily pass anonymous types around. You can either create a class, or since your data has only two properties, use a Tuple:
select new Tuple<List<string>, string> (f, target);
If I have the data types correct, then the data type of the parameter would be:
IEnumerable<Tuple<List<string>, string>>
and you would reference F and T using the Tuple properties Item1 and Item2.
1) Just to pass the result of the query, make your function generic, that will do:
var data = from lines in File.ReadAllLines(TrainingDataFile)
.Skip(ContainsHeader ? 1 : 0)
let f = lines.Split(new[] { FieldSeparator }).ToList<String>()
let target = f[TargetVariablePositionZeroBased]
select new { F=f, T=target };
SomeMethod(data);
public void SomeMethod<T>(IEnumerable<T> enumerable)
{
// ^^choose the return type..
}
Simple. If the processing inside the method is something so simple this will do. But you won't be able to access properties F and T inside the method.
To do so:
2) You can use the "cast by example" trick shown here by Eric. To quote him:
We use method type inference and local variable type inference to tell
the compiler "these two things are the same type". This lets you
export an anonymous type as object and cast it back to anonymous type.
...the trick only works if the example and the source objects were
created in code in the same assembly; two "identical" anonymous types
in two different assemblies do not unify to be the same type.
SomeMethod(data);
public void SomeMethod(IEnumerable<object> enumerable)
{
var template = new { F = new List<string>(), T = string.Empty };
foreach (var item in enumerable)
{
var anonymousType = item.CastToTypeOf(template);
//print string.Join(", ", anonymousType.F) + " - " + anonymousType.T //compiles
//or whatever
}
}
//a more generic name perhaps is 'CastToTypeOf' as an extension method
public static T CastToTypeOf<T>(this object source, T example) where T : class
{
return (T)source;
}
The catch here is that SomeMethod now is tailor made for your anonymous type, since you're specifying a specific type inside the method, so its better to not make the function generic (though you can do) and to give a suitable name for the function.
3) If function is just for your unique type now, I would better have them all wrapped in a single method and not pass at all - no hassle! :)
4) Or you can delegate the action to be done on your anonymous type. So method signature would be like:
SomeMethod(data, d => print string.Join(", ", d.F) + " - " + d.T);
public void SomeMethod<T>(IEnumerable<T> enumerable, Action<T> actor)
{
foreach (var item in enumerable)
actor(item);
}
If it matters you can have Func delegate as well by having one more type argument.
5) Rely on fiddly reflection to get the properties from your anonymous type otherwise.
6) Use dynamic keyword on method argument and now you have dynamic typing. Both the above doesnt give you benefits of static typing.
7) You will be better off having a separate class that holds F and T. And that the best of all. But ask yourself do they together represent something as an entity?
8) If not, just pass an IEnumerable<Tuple> or IDictionary depending on what matters.
It all depends on what/how you want to achieve with the method. Personally, I would go for the approach 2 in a hobby project (for the fun involved), but in production code 3, 4, 7, 8 depending on the context.

Why params does not accept generic types?

Here is my scenario,
A function :
public String StringConcat(params String[] parameter)
{
String l_strReturnValue = String.Empty;
for (Int32 l_nIndex = 0; l_nIndex < parameter.Length; l_nIndex++)
{
l_strReturnValue += parameter[l_nIndex];
}
return l_strReturnValue;
}
And i'm calling it like
List<String> l_lstTest = new List<string> { "A", "B", "C" };
String l_strString = StringConcat(l_lstTest.Select(X => X).ToArray());
it returns the value as "ABC"
But its showing error if I call the function without type convrsion like
String l_strString = StringConcat(l_lstTest.Select(X => X));
So how to use the function without conversion ?
Note 1 :
In XDocument Add method - they have used like params, but there is no such needs of type conversions.
Note 2 :
The purpose of this post is not to add the strings, just want to learn more about the limits of params.
The return type of Select(X => X) will be IEnumerable<string> - not an array. So you need another overload:
public String StringConcat(IEnumerable<string> parameter)
You'd probably make the array overload call this overload.
(And yes, obviously you'd want to use StringBuilder instead of repeated string concatenation - and foreach instead of a for loop.)
Note that the relevant XDocument.Add overload takes a params Object[] parameter, not params String[] - and LINQ to XML works such that if you try to add something which is itself enumerable, it's as if you added each item in turn. That's not part of the language - it's part of the implementation. So if you call:
doc.Add(someStringArray.Select(x => x))
that will actually just call XDocument.Add(object) which will notice that the argument implements IEnumerable.
The limits of the params keyword is that the parameter must be an array type.
l_lstTest.Select(X => X) is an IEnumerable<string>, not an array, so it does not match the formal parameter type. It doesn't work for the same reason it would not work if you tried to pass a plain int.
To make it work, you should add another overload of the method:
public String StringConcat(IEnumerable<string> parameter)
{
// your code here
}
It's a limitation of params by design, because if you were using
public String StringConcat(params object[] parameter)
and you called with IEnumerable, you couldn't figure out if the parameter is a single parameter or it should iterate on the enumerable. For instance, without this limitation, in
StringConcat( l_lstTest.Select(X => X) )
the IEnumerable should be an element of the list of parameters or it's the list of parameters?

Get Method Name Using Lambda Expression

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.

Categories