Im пытается «Func» делегирование.C# Func <> типы аргументов
Func<int, string[], bool>(myFunc); //works OK
Func<string[], bool>(myFunc); //Exception ???
Необработанное исключение: System.ArgumentException: Объект «System.String'cannot быть преобразован в тип 'System.String []'. Похоже, "Func" не нравится тип аргумента "строка []"?!?»
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Delegate> commandHash = new Dictionary<string, Delegate>();
commandHash.Add("Test", new Func<string[], bool>(Test));
string[] str = { "justtext" };
try
{
commandHash["Test"].DynamicInvoke(str);
}
catch
{
Console.WriteLine("Exception");
}
}
static bool Test(string[] s)
{
Console.WriteLine("Test");
return true;
}
}
}
// CODE which works OK, what Im missing ?!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Delegate> commandHash = new Dictionary<string, Delegate>();
commandHash.Add("Test", new Func<string[], int, bool>(Test));
string[] str = { "justtext" };
try
{
commandHash["Test"].DynamicInvoke(str, 1);
}
catch
{
Console.WriteLine("Exception");
}
}
static bool Test(string[] s, int i)
{
Console.WriteLine("Test");
return true;
}
}
}
Что такое 'myfunc'? –
Нет; это означает, что вы пытаетесь сделать недействительный листинг. Посмотрите на трассировку стека – SLaks
myFunc (string {} s), но он не работает с делегатом. – kestasj