你好我有这个代码:
# Note: This must be an executable or DLL compiled for .NET
$Path = "C:\Users\sadettin\desktop\tok.exe"
# Get Base64-encoded representation of the bytes that make up the assembly.
$bytes = [System.IO.File]::ReadAllBytes($Path)
$string = [System.Convert]::ToBase64String($bytes)
# ...
# Convert the Base64-encoded string back to a byte array, load
# the byte array as an assembly, and save the object representing the
# loaded assembly for later use.
$bytes = [System.Convert]::FromBase64String($string)
$assembly = [System.Reflection.Assembly]::Load($bytes)
# Get the static method that is the executable's entry point.
# Note:
# * Assumes 'Program' as the class name,
# and a static method named 'Main' as the entry point.
# * Should there be several classes by that name, the *first*
# - public or non-public - type returned is used.
# If you know the desired type's namespace, use, e.g.
# $assembly.GetType('MyNameSpace.Program').GetMethod(...)
$entryPointMethod =
$assembly.GetTypes().Where({ $_.Name -eq 'Program' }, 'First').
GetMethod('Main', [Reflection.BindingFlags] 'Static, Public, NonPublic')
# Now you can call the entry point.
# This example passes two arguments, 'foo' and 'bar'
$entryPointMethod.Invoke($null, (, [string[]] ('foo', 'bar')))
该代码适用于 .NET Framework 控制台项目。不知何故,它不适用于 .NET Framework Form 应用程序,我添加了这个:
$entryPointMethod.Invoke($null, $null)
目前,它适用于控制台和表单应用程序版本。
但是当我尝试将另一个 .net 程序放入 $path 时它不会加载,我认为这是因为 $entryPointMethod。所以我们需要修改它以使其适用于所有程序。
有办法让这个系统通用吗???以及如何做?谢谢
入口点不需要在
Program
类中(技术上什至不需要调用 Main
,尽管这在 C# 中是强制执行的)。
获取入口点函数的正确方法是使用程序集的
EntryPoint
属性
# This example passes two arguments, 'foo' and 'bar'
$assembly.EntryPoint.Invoke($null, @('foo', 'bar'));
这仅适用于可执行文件
.exe
。如果它是图书馆.dll
那么它将没有入口点。