Board logo

标题: [原创代码] powershell与C#交互 [打印本页]

作者: yiwuyun    时间: 2015-12-1 16:00     标题: powershell与C#交互

  1. if ($true){}# == ($true){}# goto ___yiwuyun
  2. <#BeginBatOperation#
  3. :___yiwuyun
  4. @echo off&setlocal&cls
  5. (echo $yiwuyun_fileName="%~f0"&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  6. pause
  7. exit/b 0
  8. #EndBatOperation#>
  9. <#StartPowerShell#>
  10. $TypeSignature=@"
  11. using System;
  12. //using System.Drawing;
  13. using System.Collections;
  14. using System.Runtime.InteropServices;
  15. public class MyObject{
  16.   private String x,y;
  17.   private int z;
  18.   public String GetString(){
  19.     return x+":"+y+"   "+"第"+z+"个";
  20.   }
  21.   public MyObject(ArrayList x,int y){
  22.     this.x=x[0].ToString();
  23.     this.y=x[1].ToString();
  24.     z=y;
  25.   }
  26. }
  27. "@
  28. Add-Type -TypeDefinition $TypeSignature;
  29. $testArrayList=New-Object System.Collections.ArrayList;
  30. $testCollect=New-Object System.Collections.ArrayList;
  31. $testArrayList.Add("当前时间是")|Out-Null;
  32. $testArrayList.Add($(Get-Date))|Out-Null;
  33. $testCollect.Add($testArrayList)|Out-Null;
  34. $testCollect.Add([int]100)|Out-Null;
  35. $myobj=New-Object MyObject -ArgumentList $testCollect;
  36. $myobj.GetString();
  37. ###该测试实例表明,Powershell向C#传递参数时,如果要传一个ArrayList对象时,要把ArrayList放在另一个ArrayList中,因为参数
  38. ###列表中New-Object MyObject -ArgumentList $testCollect要求传一个数组到C#,ArgumentList有多少个对象,就表明C#需要多少个
  39. ###参数。
  40. <#EndPowerShell#>
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2