void foo(int one, int two) {
}
public static void Main(string[] args) {
var bar = new int[] { 1, 2 };
foo(params bar);
}
What's the correct syntax to deconstruct the bar array and pass it as the arguments to the foo method?
In some other languages you can use a splat operator foo(...bar) or an unpack operator foo(*bar).
How can I do it in C#?
There isn't an equivalent function in C#. Each argument has to be passed individually.
There are, of course, work arounds that you likely already know. You could declare an overload for your function that would accept an array and call the original function using the first two inputs. The other alternative that I can think of is to declare the function parameter with the params keyword so that it could receive an array or multiple conma-separated elements when called.
void foo(params int[] numbers)
{ // TODO: Validate numbers length
int one = numbers[0];
int two = numbers[1];
}
public static void Main(string[] args) {
var bar = new int[] { 1, 2 };
// both valid function calls below
foo(bar);
foo(bar[0], bar[1]);
}
You can always use Reflection for such purpose.
Here is example snippet on your example method:
class MainClass
{
void foo(int one, int two)
{
Console.WriteLine(one + two);
}
static void Main()
{
var myInstance = new MainClass();
var bar = new object[] { 1, 2 };
var method = myInstance.GetType().GetMethod(nameof(MainClass.foo), BindingFlags.NonPublic | BindingFlags.Instance)
?? throw new InvalidOperationException($"Method '{nameof(MainClass.foo)}' not found");
method.Invoke(myInstance, bar) ;
}
}
Related
The title is may be not clear enough, but I'll show you a short program in order to make you understand what I want to do:
Class Program
{
private static Obj A = new Obj(...);
private static void Function(AnyMethodOfMyObject() m)
{
object[] result = A.m();
...
}
static void main()
{
double a,b,c = 0;
string d = " ";
Function(MethodX(a,b,c));
Function(MethodY(d,a,b));
...
}
}
The methods will always return the same type which is an object[], but I don't have the same number/type of argument.
Thanks !
You can do this simply with a Func<object[]>:
class Program
{
private static Obj A = new Obj(...);
private static void Function(Func<object[]> m)
{
object[] result = m();
...
}
static void main()
{
double a,b,c = 0;
string d = " ";
Function(() => A.MethodX(a,b,c));
Function(() => A.MethodY(d,a,b));
...
}
}
Func<object[]> is a delegate (broadly, a reference to a method) which does not take any parameters, and which returns an array of objects. While MethodX takes 3 parameters (a,b,c), we can create a new anonymous method without doesn't take any parameters itself, and which just calls MethodX and passes in the values of a, b and c (they're captured at the point that we create this new anonymous method). This is what () => MethodX(a, b, c) does.
If you have different Obj instances and you want to control which one the method is called on, then use a Func<Obj, object[]>. This takes an Obj as a parameter, and returns an object[] as before.
class Program
{
private static Obj A = new Obj(...);
private static void Function(Func<Obj, object[]> m)
{
object[] result = m(A);
...
}
static void main()
{
double a,b,c = 0;
string d = " ";
Function(x => x.MethodX(a,b,c));
Function(x => x.MethodY(d,a,b));
...
}
}
I am new to Lambda's and Delegates. I think my question may not be a good question but i am trying to write a simple Custom Predicate that act just like a Built-In Prediciate.
So i am going to share my Code: Please share with me that where i am going to make a mistake:
Built-In Predicate Code Example:
namespace Built_In_Predicate
{
class Program
{
static void Main(string[] args)
{
List<string> _ListOfPlayers = new List<string>()
{
"James Anderson",
"Broad",
"foo"
};
// Method 1. Predicate and Anonymous function.
Predicate<string> _Predicate = delegate (string someString) { return someString.Length == 3; };
string result = _ListOfPlayers.Find(_Predicate);
Console.WriteLine("Result : {0}", result);
}
}
}
Trying to Create a Custom Predicate (Code):
namespace CustomPredicate
{
class Program
{
// Delegate (Takes some string as a Input and return a Boolean.)
public delegate bool CustomPredicate(string someString);
static void Main(string[] args)
{
List<string> _ListOfPlayers = new List<string>()
{
"James Anderson",
"Broad",
"foo"
};
// Instance of CustomPredicate.
CustomPredicate customPredicate = delegate (string someString) { return someString.Length == 3; };
string result = _ListOfPlayers.Find(customPredicate); // its error.
}
}
}
Help will be appreciated.
Delegates cannot be implicitly converted to each other even if they have the same signature.
Find expects a System.Predicate<T> so you have to give it a System.Predicate<T>.
You can write your own Find method if you want to use your own CustomPredicate.
There are also ways to use your customPredicate variable in the call to Find:
_ListOfPlayers.Find(new Predicate<string>(customPredicate));
_ListOfPlayers.Find(customPredicate.Invoke);
You cannot call Find with something else as the type Predicate. But if you want your own delegate, you could call FirstOrDefault (System.Linq) and then use it.
private delegate bool CustomPredicate (string t);
static void Main(string[] args)
{
List<string> _ListOfPlayers = new List<string>()
{
"James Anderson",
"Broad",
"foo"
};
// Method 1. Predicate and Anonymous function.
CustomPredicate _Predicate = delegate (string someString) { return someString.Length == 3; };
string result = _ListOfPlayers.FirstOrDefault(x => _Predicate(x));
Console.WriteLine("Result : {0}", result);
Console.ReadLine();
}
I used a reflection to get a return value of type object, but its actual type is (int[], string[]) I have double check with obj.GetType().ToString() and it prints System.ValueTuple`2[System.Int32[],System.String[]].
But just casting with ((int[], string[]))obj or (ValueTuple<int[],string[]>)obj return that the cast is invalid. How to correctly do this?
Ok I finally got it working.
It is possible to further reflect the ValueTuple's Field ItemX. Not sure if there are other less forced ways where we can get the tuple as a whole.
using System;
namespace CTest
{
class Program
{
static void Main(string[] args)
{
Test t = new Test();
var tuple = typeof(Test).GetMethod(nameof(t.ReturnTuple)).Invoke(t, null);
int[] i = (int[])typeof((int[], string[])).GetField("Item1").GetValue(tuple);
string[] s = (string[])typeof((int[], string[])).GetField("Item2").GetValue(tuple);
foreach (int data in i)
{
Console.WriteLine(data.ToString());
}
foreach (string data in s)
{
Console.WriteLine(data);
}
// Output :
// 1
// 2
// 3
// a
// b
// c
}
}
class Test
{
public (int[], string[]) ReturnTuple() => (new int[] { 1, 2, 3 }, new string[] { "a", "b", "c" });
}
}
Wondering why this doesn't work. Insight appreciated.
static void Main(string[] args)
{
List<int> foo = new List<int> { 1, 2, 3 };
var myResult = MyTest<int>(foo);
}
private static List<int> MyTest<T>(List<T> input)
{
List<int> bar = new List<int> { 2, 3, 4 };
return bar.Where(b => input.Contains(b)).ToList();
}
Expected output from MyTest() is a List { 2, 3 }. However, the compiler reports two errors on input.Contains(b), as follows:
Argument 1: cannot convert from 'int' to 'T'
The best overloaded method match for 'System.Collections.Generic.List.Contains(T)' has some invalid arguments
This Where() clause works fine if I don't use generic lists.
This is a simplification of my real-world problem, so please don't get stuck on "why are you writing this?" The problem is the error and why it's occurring.
Revised for (hopefully) clarity:
namespace SandBox
{
class Foo
{
public int FooInt { get; set; }
public string FooString { get; set; }
}
class Program
{
private static List<Foo> fooList = new List<Foo> {
new Foo() {FooInt = 1, FooString = "A"},
new Foo() {FooInt = 2, FooString = "B"},
new Foo() {FooInt = 3, FooString = "C"}
};
static void Main(string[] args)
{
List<int> myIntList = new List<int> { 1, 2 };
var myFirstResult = GetFoos<int>(myIntList);
List<string> myStringList = new List<string> { "A", "B" };
var mySecondResult = GetFoos<string>(myStringList);
}
/// <summary>
/// Return a list of Foo objects that match the input parameter list
/// </summary>
private static List<Foo> GetFoos<T>(List<T> input)
{
//***
// Imagine lots of code here that I don't want to duplicate in
// an overload of GetFoos()
//***
if (input is List<int>)
{
//Use this statement if a list of integer values was passed in
return fooList.Where(f => input.Contains(f.FooInt));
}
else if (input is List<string>)
{
//Use this statement if a list of string values was passed in
return fooList.Where(f => input.Contains(f.FooString));
}
else
return null;
}
}
}
The same compiler errors are reported on input.Contains(f.Property).
input should be a List<int>
and then, whenever you call the function, if T is not an int, you'll know that it will always return an empty list anyway.
the function doesn't make very much sense when T is not an int.
also another solution
static void MainT(string[] args)
{
List<int> foo = new List<int> { 1, 2, 3 };
var myResult = MyTest<int>(foo);
}
private static List<int> MyTest<T>(List<T> input) where T : IEquatable<int>
{
List<int> bar = new List<int> { 2, 3, 4 };
return bar.Where(b => input.Any(i => i.Equals(b))).ToList();
}
Just look at this function in isolation.
private static List<int> MyTest<T>(List<T> input)
{
List<int> bar = new List<int> { 2, 3, 4 };
return bar.Where(b => input.Contains(b)).ToList();
}
What if T were object... or string.. there's nothing stopping T from being those types. If T were one of those types, the statement input.Contains(b) would not make sense.
The compiler is complaining because you are allowing types that do not make sense with the statements in the method body.
Try this:
static void Main(string[] args)
{
List<int> foo = new List<int> { 1, 2, 3 };
var myResult = MyTest<int>(foo);
}
private static List<int> MyTest<T>(List<T> input)
{
List<int> bar = new List<int> { 2, 3, 4 };
return bar.Where(b => input.OfType<int>().Contains(b)).ToList();
}
The problem is that at the compiler has no idea what type T is. Since T could be anything you can't call a method which expects an int (input.Contains).
The compiler can't guarantee that any <T> -- any <T> , DateTime, object, whatever -- will be castable to an int. That's why you're getting your first error.
In some circumstance you might be able to specify a kind of object in the signature of your function:
private static List<int> MyTest<T>(List<T> input) where T : someObject
This won't work for your case because int is a struct. You can work with it in a lot of other ways (other answers given outline some great methods), but you'll have to adjust your current strategy in some way.
I am using reflection class to invoke some methods which are on the some other dll.
And one of the methods' parameters are type of delegate.
And I want to invoke this methods by using reflection.
So I need to pass function parameters as object array, but I could not find anything about
how to convert delegate to object.
Thanks in advance
A delegate is an object. Just create the expected delegate as you would normally, and pass it in the parameters array. Here is a rather contrived example:
class Mathematician {
public delegate int MathMethod(int a, int b);
public int DoMaths(int a, int b, MathMethod mathMethod) {
return mathMethod(a, b);
}
}
[Test]
public void Test() {
var math = new Mathematician();
Mathematician.MathMethod addition = (a, b) => a + b;
var method = typeof(Mathematician).GetMethod("DoMaths");
var result = method.Invoke(math, new object[] { 1, 2, addition });
Assert.AreEqual(3, result);
}
Instances of delegates are objects, so this code works (C#3 style) :
Predicate<int> p = (i)=> i >= 42;
Object[] arrayOfObject = new object[] { p };
Hope it helps !
Cédric
Here's an example:
class Program
{
public delegate void TestDel();
public static void ToInvoke(TestDel testDel)
{
testDel();
}
public static void Test()
{
Console.WriteLine("hello world");
}
static void Main(string[] args)
{
TestDel testDel = Program.Test;
typeof(Program).InvokeMember(
"ToInvoke",
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null,
null,
new object[] { testDel });
}
}
I think this blog post:
C# Reflection - Dealing with Remote Objects
answers your question perfectly.
you can see a delegate as variable type "function". the delegate describes the parameters and return value for a matching function.
delegate void Foo(int a); // here a new delegate obj type Foo has been declared
the above example allows 'Foo' to be used as a data type, the only allowed object that can be matched with a variable of type Foo data type is a method with the same signature so:
void MyFunction(int x);
Foo D = MyFunction; // this is OK
void MyOtherFunction(string x);
Foo D = MyOtherFunction; // will yield an error since not same signature.
Once you have assigned a method to a delegate, you can invoke the method via the delegate:
int n = 1;
D( n ); // or D.Invoke( n );