在PowerShell中调用C#代码创建对象类型
来源:互联网 发布:智能家居网络协议 编辑:程序博客网 时间:2024/05/17 04:05
在PowerShell中可以使用多种方式来创建自定义类型,在很少之前我有写过一些以最通用的方式来创建PowerShell类型。
今天我们要说下如何用C#代码的方式来创建对象,虽然以这种方式创建对象很少会被用到,但是如果对于设置一些对象类型来说,这种方式或许能更加深入以及灵活些,让我们一起来看下,下面的例子:
$CScode = @"public class MyCustomObject{ public string OSVersion{get; set;} public string Model {get; set;}}"@Add-Type -TypeDefinition $CScode -Language CSharpVersion3$OperatingSystem = Get-WmiObject -Class win32_OperatingSystem -ComputerName localhost$ComputerSystem = Get-WmiObject -Class win32_ComputerName -ComputerName localhost$props = @{OSVersion = $OperatingSystem.version Model = $ComputerSystem.model }$Objs = New-Object -TypeName MyCustomObject -Property $props$Objs
其实很简单,我们使用here string内部定义了一个以C#代码为基础的类,名为MyCustomObject,这个类型里面包含了一些我们自己定义的属性,全都是以string字符串为主。
然后我们使用add-type cmdlet来加载我们自己定义的代码段:
Add-Type -TypeDefinition $CScode -Language CSharpVersion3
等添加自定义类型后,我们就可以按照平时定义对象的方式给我们需要输出的属性类型赋值了:
$OperatingSystem = Get-WmiObject -Class win32_OperatingSystem -ComputerName localhost$ComputerSystem = Get-WmiObject -Class win32_ComputerName -ComputerName localhost$props = @{OSVersion = $OperatingSystem.version Model = $ComputerSystem.model }$Objs = New-Object -TypeName MyCustomObject -Property $props
基本就是这样,这个方法虽然会有那么一点麻烦,不过如果对于我们创建一些特有的属性还是十分有用的。
0 0
- 在PowerShell中调用C#代码创建对象类型
- 在C#中调用Powershell Cmdlets
- 在C#中创建类型
- 在Powershell中调用VBScript
- 在PowerShell中使用枚举类型
- 在 PowerShell 中使用 WMI 远程调用
- 在 C# 中创建 COM 对象
- 在C#中调用Ruby代码
- 在C#中调用GAMS代码
- C# 调用 PowerShell
- PowerShell中调用Win32API
- c# ,在Oracle 中,对 blob 类型对象的操作
- [原创]在PowerShell中实现Ruby字符串类型方法swapcase
- 在C#代码中执行BCS外部内容类型方法
- 在c#中通过http对象编程实现webservice调用
- 在C#中禁止某种类来创建对象
- C#调用PowerShell管理Exchange2010
- C# 调用powershell 执行命令
- RHEL5中tftp服务器的安装与配置
- Android之界面刷新(invalidate和postInvalidate使用)
- ZJU-PAT 1070. Mooncake (25)
- JAVA 位运算符和位移运算符
- hdu4845 BFS
- 在PowerShell中调用C#代码创建对象类型
- MyEclipse下开发Web Service(Axis)
- GitHub for windows
- windows编译ffmpeg
- java.lang.ClassNotFoundException: javax.persistence.Table
- 独立博客
- Python产生随机数(转)
- Linux中export 命令及LD_LIBRARY_PATH 变量
- sizeof 与 strlen 的区别