如何在代码中加载程序集并将值传递给main方法上的字符串数组args [英] How do I load the Assembly in code and pass values to the string array args on the main method

查看:103
本文介绍了如何在代码中加载程序集并将值传递给main方法上的字符串数组args的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用代码加载程序集并将值传递给Stirng数组args到main方法,

这就是我得到的

I want to load the Assembly in code and pass value to Stirng array args to the main method,

This is what i got

Assembly assembly = Assembly.LoadFrom("ConsoleApp.exe");
Type[] types= assembly.GetTypes();
foreach (Type t in types)
{
 MethodInfo method = t.GetMethod("Main",
     BindingFlags.Static | BindingFlags.NonPublic);
 if (method != null)
 {
     method.Invoke(null, null);
 }
}


这就是我的想法


This is what i Think

<pre lang="c#">Assembly assembly = Assembly.LoadFrom("ConsoleApp.exe" - MyragsVal);
Type[] types= assembly.GetTypes();
foreach (Type t in types)
{
 MethodInfo method = t.GetMethod("Main "MyragsVal"",
     BindingFlags.Static | BindingFlags.NonPublic);
 if (method != null)
 {

     method.Invoke(null, null);
   
 }
}



还是应该像这样



or should it be likes this

<pre lang="c#">Assembly assembly = Assembly.LoadFrom("ConsoleApp.exe");
Type[] types= assembly.GetTypes();
foreach (Type t in types)
{
 MethodInfo method = t.GetMethod("Main("MyragsVal")",
     BindingFlags.Static | BindingFlags.NonPublic);
 if (method != null)
 {

     method.Invoke(null, null);
   
 }
}

推荐答案

参数进入Invoke方法,它们是第二个参数.
The parameters go into the Invoke method, they are the second parameter.
string[] args = ...;
method.Invoke(null, args);


这篇关于如何在代码中加载程序集并将值传递给main方法上的字符串数组args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆