【问题标题】:Add-Type cmdlet: is it possible to point the set of the DLL files instead of Assembly names?Add-Type cmdlet:是否可以指向 DLL 文件集而不是程序集名称?
【发布时间】:2016-05-12 21:09:56
【问题描述】:

我需要在 PowerShell 中启动托管在 AutoCAD 中的 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);
        }
    }
}

但我得到错误:

“DatabaseServices”的类型或命名空间的名称在 “Autodesk.AutoCAD”的命名空间(通过了程序集引用?)。
“应用程序”的类型或命名空间的名称在 “Autodesk.AutoCAD.ApplicationServices”的命名空间(程序集 引用通过了?)

我该如何解决?

【问题讨论】:

    标签: c# powershell powershell-4.0 powershell-hosting


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-30
      • 2012-10-29
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 2019-10-02
      • 2012-12-14
      相关资源
      最近更新 更多