批处理之家's Archiver

yiwuyun 发表于 2015-12-1 16:00

powershell与C#交互

[code]if ($true){}# == ($true){}# goto ___yiwuyun
<#BeginBatOperation#
:___yiwuyun
@echo off&setlocal&cls
(echo $yiwuyun_fileName="%~f0"&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
pause
exit/b 0
#EndBatOperation#>

<#StartPowerShell#>

$TypeSignature=@"
using System;
//using System.Drawing;
using System.Collections;
using System.Runtime.InteropServices;
public class MyObject{
  private String x,y;
  private int z;
  public String GetString(){
    return x+":"+y+"   "+"第"+z+"个";
  }
  public MyObject(ArrayList x,int y){
    this.x=x[0].ToString();
    this.y=x[1].ToString();
    z=y;
  }
}
"@
Add-Type -TypeDefinition $TypeSignature;
$testArrayList=New-Object System.Collections.ArrayList;
$testCollect=New-Object System.Collections.ArrayList;
$testArrayList.Add("当前时间是")|Out-Null;
$testArrayList.Add($(Get-Date))|Out-Null;
$testCollect.Add($testArrayList)|Out-Null;
$testCollect.Add([int]100)|Out-Null;

$myobj=New-Object MyObject -ArgumentList $testCollect;
$myobj.GetString();

###该测试实例表明,Powershell向C#传递参数时,如果要传一个ArrayList对象时,要把ArrayList放在另一个ArrayList中,因为参数
###列表中New-Object MyObject -ArgumentList $testCollect要求传一个数组到C#,ArgumentList有多少个对象,就表明C#需要多少个
###参数。

<#EndPowerShell#>[/code]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.