C# Add-Type cmdlet:是否可以指向DLL文件集而不是程序集名称?
我需要在承载在AutoCAD内部的PowerShell中启动cmdled。AutoCAD的程序集(它是PowerShell的主机)不在GAC中。如何正确指向AutoCAD的部件?是否可以指向DLL文件集而不是程序集名称?当前AppDomain中已加载所有必需的程序集C# Add-Type cmdlet:是否可以指向DLL文件集而不是程序集名称?,c#,powershell,powershell-4.0,powershell-hosting,C#,Powershell,Powershell 4.0,Powershell Hosting,我需要在承载在AutoCAD内部的PowerShell中启动cmdled。AutoCAD的程序集(它是PowerShell的主机)不在GAC中。如何正确指向AutoCAD的部件?是否可以指向DLL文件集而不是程序集名称?当前AppDomain中已加载所有必需的程序集 $asm = ([System.AppDomain]::CurrentDomain.GetAssemblies()) | where { ` ($_.FullName).StartsWith("acdbmgd") -or (
$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”(部件)的命名空间 引用是否通过?)
我怎样才能修复它?是的,这是可能的。您需要使用
Assembly
对象的Location
属性:
$asm = @(
[System.AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object {
$_.FullName.StartsWith("acdbmgd") -or
$_.FullName.StartsWith("acmgd") -or
$_.FullName.StartsWith("accoremgd")
} |
Select-Object -ExpandProperty Location
)