Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

c# - 对在PowerShell中添加程序集以使用.NET类感到困惑(Confused about adding assemblies in PowerShell for using .NET Classes)

I would appreciate some clarification.

(我希望澄清一下。)

When using a .NET Framework Class in PowerShell we sometimes have to load the Assembly into memory we have to use the Add-Type Cmdlet in PowerShell.

(在PowerShell中使用.NET Framework类时,有时必须将程序集加载到内存中,而在PowerShell中则必须使用Add-Type Cmdlet。)

So for instance if I wanted to use the System.ServiceProccess.ServiceController Class I would do something like this

(因此,例如,如果我想使用System.ServiceProccess.ServiceController类,我会做类似的事情)

Add-Type -AssemblyName System.ServiceProcess

But here is where I get confused.

(但是,这是我感到困惑的地方。)

The documentation says there are 2 assemblies for the System.ServiceProcess.ServiceController Class

(该文档说System.ServiceProcess.ServiceController类有2个程序集)

https://docs.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontroller?view=netframework-4.8 namely: System.ServiceProcess.ServiceController.dll, and System.ServiceProcess.dll

(https://docs.microsoft.com/zh-cn/dotnet/api/system.serviceprocess.servicecontroller?view=netframework-4.8即: System.ServiceProcess.ServiceController.dllSystem.ServiceProcess.dll)

It seems to work no matter which one I add but I would like to know what is happening under the hood of the bonnet.

(无论我添加哪一个,它似乎都可以工作,但是我想知道在引擎盖下发生了什么。)

Even more confusing is the Process class : https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=netframework-4.8

(Process类更加令人困惑: https : //docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=netframework-4.8)

Here we have 3 assemblies : System.Diagnostics.Process.dll, System.dll and netstandard.dll

(这里我们有3个程序集: System.Diagnostics.Process.dll,System.dllnetstandard.dll)

I can only load System.Dll.

(我只能加载System.Dll。)

The other 2 it simply says the assembly can't be found.

(其他2只表示找不到该程序集。)

So the question is, if I want to use and .NET Framework Class I Assume I should have all the aasemblies so how do I load them all in PowerShell?

(所以问题是,如果我想使用.NET Framework类,我应该拥有所有组件,那么如何在PowerShell中将它们全部加载?)

Hope this is clear and thanks for an answer.

(希望这很清楚,并感谢您的答复。)

Kind regards,

(亲切的问候,)

John

(约翰)

  ask by John translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

System.ServiceProcess.dll is in the GAC, and part of the .NET Framework.

(System.ServiceProcess.dll在GAC中,并且是.NET Framework的一部分。)

System.ServiceProcess.ServiceController.dll is part of the Core CLR.

(System.ServiceProcess.ServiceController.dll是Core CLR的一部分。)

But wait a minute - PowerShell is based on the .NET Framework CLR.

(但是请稍等-PowerShell基于.NET Framework CLR。)

If you build your assembly against the correct netstandard profile , and its dependencies are resolvable, it should be able to run on the .NET Framework.

(如果您根据正确的netstandard配置文件构建程序集,并且其依赖项是可解决的,则它应能够在.NET Framework上运行。)

You can see that System.ServiceProcess.dll references kernel32 , while System.ServiceProcess.ServiceController.dll references api-ms-win-service-core .

(您可以看到System.ServiceProcess.dll引用kernel32 ,而System.ServiceProcess.ServiceController.dll引用api-ms-win-service-core 。)

It's sort of the same, but the Core CLR version will have presumably less functionality.

(差不多 ,但是Core CLR版本的功能可能更少 。)

As for the others, most of the System namespace resides in mscorlib and System.dll .

(至于其他的,大多数System名称空间都位于mscorlibSystem.dll 。)

In recent years, there has been an effort to make the core library leaner by moving parts of it out, to have out-of-band delivery.

(近年来,人们一直在努力通过将部分核心库移出以进行带外交付来使其更精简 。)

For example, System.Diagnostics.Process.dll now ships as a nuget package , which enables faster rollouts to users rather than shipping a major .NET Framework update that targets are required to install.

(例如, System.Diagnostics.Process.dll现在作为nuget软件包提供 ,从而可以更快地向用户推出,而不是交付需要安装目标的主要.NET Framework更新。)

If you're using regular Powershell and not Powershell Core , you can just reference .NET Framework assemblies and not see confusing

(如果您使用的是常规Powershell而不是Powershell Core ,则可以仅引用.NET Framework程序集,而不会混淆)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...