C#无法从'System.DateTime'转换为'object []'methodinfo.invoke

Gle*_*son 2 c# datetime createinstance invoke

看起来我可能以错误的方式接近这个方向,而且方向非常受欢迎.

我试图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);
}
Run Code Online (Sandbox Code Playgroud)

dot*_*tom 8

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

methodInfo.Invoke(o, new object[] { scheduleDate });
Run Code Online (Sandbox Code Playgroud)