如何正确添加.NET程序集到Powershell会话?

我有一个.NET程序集(一个DLL),这是一个API来备份我们在这里使用的软件。 它包含了一些我想在Powershell脚本中使用的属性和方法。 但是,我遇到了很多问题,首先加载程序集,然后使用任何types的程序集加载。

完整的文件path是:

C:\rnd\CloudBerry.Backup.API.dll 

PowerShell中,我使用:

 $dllpath = "C:\rnd\CloudBerry.Backup.API.dll" Add-Type -Path $dllpath 

我得到下面的错误:

 Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. At line:1 char:9 + Add-Type <<<< -Path $dllpath + CategoryInfo : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException + FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeComma ndAdd-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. 

在另一个.NET程序集上使用相同的cmdlet, DotNetZip ,它在网站上使用相同的function的例子也不适合我。

我最终发现我似乎能够使用reflection来加载程序集:

 [System.Reflection.Assembly]::LoadFrom($dllpath) 

虽然我不明白Load方法,LoadFrom方法或LoadFile方法之间的区别,但最后一种方法似乎可行。

但是,我似乎仍然无法创build实例或使用对象。 每次尝试时,都会收到描述Powershell无法find任何公共types的错误。

我知道这些课程在那里:

 $asm = [System.Reflection.Assembly]::LoadFrom($dllpath) $cbbtypes = $asm.GetExportedTypes() $cbbtypes | Get-Member -Static 

—-节选开始—-

  TypeName: CloudBerryLab.Backup.API.BackupProvider Name MemberType Definition ---- ---------- ---------- PlanChanged Event System.EventHandler`1[CloudBerryLab.Backup.API.Utils.ChangedEventArgs] PlanChanged(Sy... PlanRemoved Event System.EventHandler`1[CloudBerryLab.Backup.API.Utils.PlanRemoveEventArgs] PlanRemoved... CalculateFolderSize Method static long CalculateFolderSize() Equals Method static bool Equals(System.Object objA, System.Object objB) GetAccounts Method static CloudBerryLab.Backup.API.Account[], CloudBerry.Backup.API, Version=1.0.0.1, Cu... GetBackupPlans Method static CloudBerryLab.Backup.API.BackupPlan[], CloudBerry.Backup.API, Version=1.0.0.1,... ReferenceEquals Method static bool ReferenceEquals(System.Object objA, System.Object objB) SetProfilePath Method static System.Void SetProfilePath(string profilePath) 

—-节选结束—-

试图使用静态方法失败,我不知道为什么!

 [CloudBerryLab.Backup.API.BackupProvider]::GetAccounts() Unable to find type [CloudBerryLab.Backup.API.BackupProvider]: make sure that the assembly containing this type is load ed. At line:1 char:42 + [CloudBerryLab.Backup.API.BackupProvider] <<<< ::GetAccounts() + CategoryInfo : InvalidOperation: (CloudBerryLab.Backup.API.BackupProvider:String) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound 

任何指导赞赏!

    你可以用try catch来包围Add-Type ,并打印LoaderExceptions属性,因为错误是要做的。 它可能会提供一个例外,更详细的错误信息。

     try { Add-Type -Path "C:\rnd\CloudBerry.Backup.API.dll" } catch { $_.LoaderExceptions | % { Write-Error $_.Message } } 

    我发现这个链接: http : //www.madwithpowershell.com/2013/10/add-type-vs-reflectionassembly-in.html

    他说,“.LoadWithPartialName”已被弃用。 因此,不是继续使用该方法实现Add-Type,而是使用静态内部表将“部分名称”转换为“全名”。 在问题中给出的示例中, CloudBerry.Backup.API.dll在PowerShell的内部表中没有条目,这就是为什么[System.Reflection.Assembly]::LoadFrom($dllpath)有效。 它不使用表来查找部分名称。

    我使用下面的设置来在PowerShell中加载一个自定义的csharp控件。 它允许在PowerShell中对控件进行自定义和使用。

    这里是博客链接

    http://justcode.ca/wp/?p=435

    这里是与源码的codeproject链接

    http://www.codeproject.com/Articles/311705/Custom-CSharp-Control-for-Powershell

    LoaderExceptions隐藏在错误logging中。 如果添加types错误是错误列表中的最后一个错误,则使用$Error[0].InnerException.LoaderExceptions来显示错误。 最有可能的是,你的图书馆依赖于尚未加载的其他图书馆。 您可以Add-Type每个Add-Type ,或者只是创build一个列表,并使用Add-Type-ReferencedAssemblies参数。

    我想现在你可能已经find了这个现象的答案。 遇到同样的问题后,我跑了这个post…我可以加载程序集,并查看程序集中包含的types,但无法从静态类instanciate它的一个实例。 是EFTIDY吗? 整洁,EFTidyNet.TidyNet.Options还是什么? Ooooo Weeee …问题…问题…可能是任何事情。 而通过DLL的静态方法和types,没有透露任何有希望的东西。 现在我感到沮丧。 我曾经在一个编译的C#程序中工作,但是为了我的使用,我想让它以一种间接的语言运行… powershell。

    我find的解决scheme,现在还在validation中,但是我很高兴,想分享一下。 构build一个小型的console.exe应用程序来执行我感兴趣的function,然后在反编译或显示IL代码的东西中查看它。 我使用了Red-Gate的reflection器和PowerShell语言生成器加载项和Wallah! 它显示了正确的构造函数string! :-) 尝试一下。 我希望这对任何面临这个问题的人都有效。