Allow the usage of out/ref parameters with 'params' in methods. #3960
Replies: 5 comments 8 replies
-
I'm not sure I understand what exactly you are suggesting. Do you mean combining multiple As for a way of invoking a method with any arguments, reflection already can use For example, instead of delegate bool TryParse<T>(string s, out T value);
…
TryParse<int> intTryParseDelegate = int.TryParse;
var parameters = new object[] { "42", null };
intTryParseDelegate.Method.Invoke(null, parameters);
Console.WriteLine($"{parameters[1].GetType()}: {parameters[1]}"); // prints "System.Int32: 42" |
Beta Was this translation helpful? Give feedback.
-
Let's say I have a method basically it would look like that This should not require any delegate declaration or involve too much reflection. That's why having some kind of support of out parameters inside You can see the DynamicMethod as this and just being called with .Invoke later.
|
Beta Was this translation helpful? Give feedback.
-
So, to be clear about what you're suggesting, it's not that the So: // given
void Foo(params int[] args) {
args[0] = 123;
}
// you can call
int x = 0;
Foo(ref x, 1, 2, 3);
// and that would lower to
int x = 0;
int[] temp = new int[] { x, 1, 2, 3 };
Foo(temp);
x = temp[0]; I used |
Beta Was this translation helpful? Give feedback.
-
Actually I'm sorry about types like byte[]. They are filled regardless of the usage of ref or out. Means passing that to such a native call method already works. It would just be nice to let it work directly with out instead of declaring a new byte[] before calling. |
Beta Was this translation helpful? Give feedback.
-
Actually, I can think of one way in which it could work. The type of the void M1(params int[] args)
{
args[0] = 123;
}
void M2(params string[] args)
{
args[0] = "foo";
}
M1(out int i1); // ok
M1(out string s1); // compile time error
M2(out int i2); // compile time error
M2(out string s2); // ok |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I currently made much use of
params object[]
and noticed that one big missing thing there is the support of of ref & out parameters. I think it would be a great addition to avoid creating many overloads and delegates for things like pinvokes.Currently I have a solution for pinvokes (syscalls) that generates a new DynamicMethod + IL that allows me to execute code like
syscall<TRet>(id, params)
where you don't need to specify any hardcoded list of arguments etc.There is no way to use ref/out parameters for things like byte arrays though.
Beta Was this translation helpful? Give feedback.
All reactions