Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell加载C#DLL,[System.Type]::GetType返回null_C#_Powershell - Fatal编程技术网

使用powershell加载C#DLL,[System.Type]::GetType返回null

使用powershell加载C#DLL,[System.Type]::GetType返回null,c#,powershell,C#,Powershell,我有这样一个简单的dotnetdll namespace ClassLibrary1 { public class Class1 { public static void Test() { Process.Start("CMD.exe", "/C calc"); } } } 当我尝试使用powershell加载此DLL时 $Path = "c:\\test\\ClassLibrary1.d

我有这样一个简单的dotnetdll

namespace ClassLibrary1
{
    public class Class1
    {
        public static void Test()
        {
           Process.Start("CMD.exe", "/C calc"); 
        }
    }
}
当我尝试使用powershell加载此DLL时

$Path      = "c:\\test\\ClassLibrary1.dll";
$Namespace = "ClassLibrary1";
$ClassName = "Class1";
$Method    = "Test";
$Arguments = $null

$Full_Path       = [System.IO.Path]::GetFullPath($Path);
$AssemblyName    = [System.Reflection.AssemblyName]::GetAssemblyName($Full_Path)
$Full_Class_Name = "$Namespace.$ClassName"
$Type_Name       = "$Full_Class_Name, $($AssemblyName.FullName)"
$Type            = [System.Type]::GetType($Type_Name)

$MethodInfo = $Type.GetMethod($Method)
$MethodInfo.Invoke($null, $Arguments)
它不起作用,因为
[System.Type]::GetType($Type\u Name)
返回了
$null

有什么想法吗?

  • 使用加载程序集

  • 加载后,要通过字符串变量将该程序集的
    [ClassLibrary1.Class1]
    类型作为
    [type]
    实例(用于反射),只需将转换为
    [type]

以下代码的更正和注释版本演示了该方法:

# Compile the C# source code to assembly .\ClassLibrary1.dll
Add-Type -TypeDefinition @'
    namespace ClassLibrary1
    {
        public class Class1
        {
            public static void Test()
            {
            System.Diagnostics.Process.Start("CMD.exe", "/C calc"); 
            }
        }
    }
'@ -OutputAssembly .\ClassLibrary1.dll  


# Define the path to the assembly. Do NOT use "\\" as the path separator.
# PowerShell doesn't use "\" as the escape character.
$Path      = ".\ClassLibrary1.dll" 
$Namespace = "ClassLibrary1"
$ClassName = "Class1"
$Method    = "Test"
$Arguments = $null

# Load the assembly by its filesystem path, using the Add-Type cmdlet.
# Use of relative paths works.
Add-Type -Path $Path

$Full_Class_Name = "$Namespace.$ClassName"

# To get type [ClassLibrary1.Class1] by its full name as a string,
# simply cast to [type]
$Type            = [type] $Full_Class_Name

$MethodInfo = $Type.GetMethod($Method)
$MethodInfo.Invoke($null, $Arguments)

至于你所尝试的

如前所述,只有在以下情况下才能找到给定类型:

  • 其程序集已加载
  • 它的程序集可以通过标准的程序集解析来定位,如详细所述,这涉及许多复杂的规则,其中只有两个可能适用于您的场景,即您试图引用
    mscorlib
    程序集中的内置类型,或位于GAC中的程序集(全局程序集缓存)。
    • 根据定义,只有强命名的程序集(使用与程序集全名中提到的公钥匹配的私钥签名的程序集)才能放置在GAC中

虽然您可以先调用
[System.Reflection.Assembly]::LoadFile($Full\u Path)
以通过其文件系统路径加载程序集,然后
[System.Type]::GetType($Type\u Name)
将成功,使用
Add Type-Path
加载程序集(其附加优点是不需要完整(绝对)文件路径),以及加载后仅使用类型的全名使用
[Type]
,最终会更简单,也更符合PowerShell的习惯用法

您没有加载程序集<代码>[System.Type]::GetType不加载程序集,除非它位于GAC或程序集搜索路径中。您可能需要按照以下说明使用
加载文件