C# Func <> 参数类型
C# Func <> argument types
Im trying "Func" delegation.
Func<int, string[], bool>(myFunc); //works OK
Func<string[], bool>(myFunc); //Exception ???
Unhandled Exception: System.ArgumentException:
Object of 'System.String'cannot be converted to type 'System.String[]'.
Looks like "Func" doesn't like argument type "string[]" ?!?"
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;
}
}
}
DynamicInvoke 参数使用 params 对象[] 定义。
这意味着当您将字符串[]数组传递给此函数时,字符串数组中的每个条目都是一个新参数。
您遇到异常,因为您无法将字符串转换为字符串[];
您需要做的就是像这样调用 DynamicInvoke。
commandHash["Test"].DynamicInvoke(new object[] { str });
Im trying "Func" delegation.
Func<int, string[], bool>(myFunc); //works OK
Func<string[], bool>(myFunc); //Exception ???
Unhandled Exception: System.ArgumentException: Object of 'System.String'cannot be converted to type 'System.String[]'. Looks like "Func" doesn't like argument type "string[]" ?!?"
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;
}
}
}
DynamicInvoke 参数使用 params 对象[] 定义。
这意味着当您将字符串[]数组传递给此函数时,字符串数组中的每个条目都是一个新参数。
您遇到异常,因为您无法将字符串转换为字符串[];
您需要做的就是像这样调用 DynamicInvoke。
commandHash["Test"].DynamicInvoke(new object[] { str });