3

看我可能以错误的方式接近这个,方向是非常受欢迎的。

我正在尝试触发Start我的解决方案中的所有方法。

Start 方法需要一个日期时间

但是,当尝试将日期作为“调用”的参数传递时,我遇到了错误

无法从 System.DateTime 转换为 object[]

欢迎任何想法

谢谢gws

scheduleDate = new DateTime(2010, 03, 11);

Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "AssetConsultants");

foreach (Type t in typelist)
{
    var methodInfo = t.GetMethod("Start", new Type[] {typeof(DateTime)} );
    if (methodInfo == null) // the method doesn't exist
    {
       // throw some exception
    }

    var o = Activator.CreateInstance(t);                 
    methodInfo.Invoke(o, scheduleDate);
}
4

2 回答 2

9

方法的第二个参数Invoke需要一个带有参数的对象数组。所以不要DateTime在对象数组中传递一个包装:

methodInfo.Invoke(o, new object[] { scheduleDate });
于 2016-08-01T04:44:28.467 回答
0

当预期参数是对象数组时,您将 DateTime 作为参数传递。

尝试以下操作:

private void button_Click(object sender, EventArgs e)
    {
        var scheduleDate = new DateTime(2010, 03, 11);

        var typelist = System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                  .Where(t => t.Namespace == "AssetConsultants")
                  .ToList();


        foreach (Type t in typelist)
        {
            var methodInfo = t.GetMethod("Start", new Type[] { typeof(DateTime) });
            if (methodInfo == null) // the method doesn't exist
            {
                // throw some exception
            }

            var o = Activator.CreateInstance(t);

            methodInfo.Invoke(o, new object[] { scheduleDate });
        }

    }
于 2016-08-01T04:47:22.500 回答