PowerShell是否支持OOP? [英] Does PowerShell support OOP?

查看:71
本文介绍了PowerShell是否支持OOP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell中的类,接口和Mixin等概念是什么?它支持OOP吗?如果是这样,我在哪里可以读到它?

What is about such concepts as Class, Interface, Mixin in PowerShell? Does it support OOP? If so, where can I read about this?

推荐答案

您可以使用Add-Type cmdlet在PowerShell v2.0中定义新类型:

You can define new types in PowerShell v2.0 using the Add-Type cmdlet:

详细说明

使用Add-Type cmdlet,您可以在Windows PowerShell会话中定义.NET类.然后,可以实例化对象(通过使用New-Object cmdlet)并使用对象,就像使用任何.NET ob一样. 主题如果将Add-Type命令添加到Windows PowerShell配置文件,则该类将在所有Windows PowerShell会话中可用.

The Add-Type cmdlet lets you define a .NET class in your Windows PowerShell session. You can then instantiate objects (by using the New-Object cmdlet) and use the objects, just as you would use any .NET ob ject. If you add an Add-Type command to your Windows PowerShell profile, the class will be available in all Windows PowerShell sessions.

您可以通过指定现有的程序集或源代码文件来指定类型,也可以在行中指定源代码或将其保存在变量中.您甚至可以只指定一个方法,然后Add-Type将定义 并生成类.您可以使用此功能对Windows PowerShell中的非托管函数进行平台调用(P/Invoke)调用.如果指定源代码,则Add-Type会编译指定的源代码 de并生成一个包含新.NET类型的内存中程序集.

You can specify the type by specifying an existing assembly or source code files, or you can specify source code in line or saved in a variable. You can even specify only a method and Add-Type will define and generate the class. You can use this feature to make Platform Invoke (P/Invoke) calls to unmanaged functions in Windows PowerShell. If you specify source code, Add-Type compiles the specified source co de and generates an in-memory assembly that contains the new .NET types.

您可以使用Add-Type的参数来指定备用语言和编译器(默认为CSharp),编译器选项,程序集依赖项,类名称空间以及类型和名称. 结果组装.

You can use the parameters of Add-Type to specify an alternate language and compiler (CSharp is the default), compiler options, assembly dependencies, the class namespace, and the names of the type and the resulting assembly.

help Add-Type了解更多信息.

另外,请参阅:

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆