【发布时间】:2021-11-19 04:27:16
【问题描述】:
我目前正在使用 PowerShell 学习反射,我正在努力寻找如何使用反射将参数传递给测试 DLL 中的静态 void 函数,已经在这几个小时并且很可能在谷歌或这里跑过了答案在 StackOverflow 上?如果这里的大师可以帮助我,那会很棒吗?
示例 DLL 代码:
using System;
using System.Diagnostics;
namespace ExampleProject
{
public class Class1
{
public static void RunProcess(string fullFileName)
{
var p = new Process
{
StartInfo =
{
FileName = fullFileName
}
}.Start();
}
}
}
当前的 Powershell 代码:
$data = "C:\temp\mytestlib.dll')
$assem = [System.Reflection.Assembly]::Load($data)
$class = $assem.GetType("ExampleProject.Class1")
$method = $class.GetMethod("RunProcess")
$fullName = "C:\\Windows\\System32\\calc.exe"
$method.Invoke($null,$fullName)
收到错误:
Exception calling "Invoke" with "2" argument(s): "Parameter count mismatch."
At line:6 char:1
+ $method.Invoke($null,$fullName)
但添加 $method.Invoke($null,$null) 允许执行 PowerShell 脚本,尽管出现静默失败?
【问题讨论】:
标签: c# powershell