2

Does C# have the convenient/standard way to "cast" function parameter list to tuples and back?

This "cast" operation if exists somewhere in .NET should be realized not on the language level (since there are no such type: "function parameter list") but somwhere in reflection libs. In forward case it return exact Type of tuple (by the function name), in backward case - call function with tuple fields.

It should look like packing/unpacking function parameter lists to tuples.

Append: Yes this is impossible in the language, but also I know/feel that this is possible using reflection and stack browsing.

2
  • Maybe I'm stupid, because I have no idea what you are talking about. Commented Nov 21, 2010 at 15:59
  • Hi, I think you are trying to do parameter packing/unpacking like in Python right? this is not posible (as far as i know) in C#. And in C# the collections (lists) are from the same type so you can't store diferent datatypes in the same collection (C# 2.0). Commented Nov 21, 2010 at 16:06

2 Answers 2

3

If this is what you would like to do:

void Foo(int a, bool b, string c)
{
    Tuple<int,bool,string> argsTuple = pack();
}                                   // ^^^^^^

... then the answer is no, you cannot conveniently convert a method's arguments to a tuple. You probably can do this with reflection, but it's not something that the .NET Framework provides out of the box.

Concerning the opposite, unpacking a tuple across a function's parameter list — i.e. Foo(argsTuple) —, you definitely cannot do that in C#.

Sign up to request clarification or add additional context in comments.

6 Comments

better: var args = GetCurrentCallParameters(); :)
I know that this is possible only using reflection (and stack browsing); but may be this is done allready in the framework.
@Roman, I suppose "better" depends on taste. While I'm usually not that explicit about types, my code example mentions the exact Tuple type in order to show what's supposed to happen; it's pack in lower-case to suggest that it's not a BCL method, but a language feature (which this kind of operation usually is, see e.g. Python).
@Roman, no, to the best of my knowledge, the .NET Framework doesn't have a method that does this.
It was a joke about "better". But actually I'm interesing not in language feature I know that this is impossible in the language, but also I know/feel that this is possible using reflection and stack browsing. If there are no such code in the framework I'm interesting actually to find those 5 people in the world who do such things :)
|
1

Unpacking can be quite simple. You can just create extension methods.

public static R Apply<T, U, R>(this Tuple<T, U> t, Func<T, U, R> f)
{
    return f(t.Item1, t.Item2);
}

public static R Apply<T, U, V, R>(this Tuple<T, U, V> t, Func<T, U, V, R> f)
{
    return f(t.Item1, t.Item2, t.Item3);
}

And so on. This can be done for actions also. Then you can use these extension methods in the following way.

tuple.Apply(Method)

1 Comment

Yes you are rigth. I was expected that unpacking could be also easy and already realised somewhere. (I had an idea to use such tricks in code generation purposes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.