添加类型 cmdlet:是否可以指向 DLL 文件集而不是程序集名称?

Add-Type cmdlet: is it possible to point the set of the DLL files instead of Assembly names?

我需要在托管在 AutoCAD 中的 PowerShell 中启动 cmdled。 AutoCAD 的程序集(它是 PowerShell 的宿主)不在 GAC 中。如何正确指向AutoCAD 的组件?是否可以指向 DLL 文件集而不是程序集名称?当前 AppDomain 中已加载所有必需的程序集。

$asm = ([System.AppDomain]::CurrentDomain.GetAssemblies()) | where { `
    ($_.FullName).StartsWith("acdbmgd") -or ($_.FullName).StartsWith("acmgd") `
    -or ($_.FullName).StartsWith("accoremgd")}

$src =[Io.File]::ReadAllText(($PSScriptRoot + "../Resources/example.cs"))

Add-Type -ReferencedAssemblies $asm -TypeDefinition $src -Language CSharp

# Launch our static `Bushman.CAD.PowerShellUtils.Example.WriteMsg()` method:
[Bushman.CAD.PowerShellUtils.Example]::WriteMsg("`nHello from CS-file!`n")

这是 ../Resources/example.cs 文件的代码:

using System;
using Autodesk.AutoCAD.Runtime;
using cad = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;


namespace Bushman.CAD.PowerShellUtils {
    public class Example {
        public static void WriteMsg(String msg) {
            Document doc = cad.DocumentManager.MdiActiveDocument;
            if (null == doc || String.IsNullOrEmpty(msg)) {
                return;
            }
            Editor ed = doc.Editor;
            ed.WriteMessage(msg);
        }
    }
}

但我收到错误:

The name of type or namespace of "DatabaseServices" is absent in a namespace of "Autodesk.AutoCAD" (the assembly reference is passed?).
The name of type or namespace of "Application" is absent in a namespace of "Autodesk.AutoCAD.ApplicationServices" (the assembly reference is passed?)

我该如何解决?

是的,这是可能的。您需要使用 Location 属性 of Assembly object:

$asm = @(
    [System.AppDomain]::CurrentDomain.GetAssemblies() |
    Where-Object {
        $_.FullName.StartsWith("acdbmgd") -or
        $_.FullName.StartsWith("acmgd") -or
        $_.FullName.StartsWith("accoremgd")
    } |
    Select-Object -ExpandProperty Location
)