如何使用 Powershell 从另一个类 (.cs) 文件读取来创建类?

发布于 2024-10-13 08:25:35 字数 1119 浏览 2 评论 0原文

我有一个 POGO(简单的推杆 getter)类,我试图在 PowerShell 中读取它

using System;
using System.Runtime.Serialization;

namespace MyApp.VM
{
  [Serializable]
  public class MyClassVM
  {
      public Int64 CtrId { get; set; }
      public string CtrName { get; set; }
      public string CtrPhone { get; set; }
      public string CtrZip { get; set; }
      public DateTime AddDate { get; set; }
  }
}

,这是尝试从文件中读取该类的 ps1 代码。

function Build-Pogo
{
    $FileDir   = "D:\YourDirectoryOfPogo" 
    $ClassName = "MyClassVM"
    $FileName  = $FileDir + "\" + $ClassName + ".cs"

    # Build the class from the file
    $AllLines = [string]::join([environment]::newline, (Get-Content $FileName))
    Add-Type -TypeDefinition $AllLines

    # spin thru each property for class
    $ClassHandle = New-Object -typeName $ClassName
    $ClassHandle | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}
}

*注意最后一行是占位符,用于稍后出现更复杂的逻辑。

对于文件中的每个获取/设置,此错误消息会在添加类型处中断。

“MyApp.VM.MyClassVM.CtrId.get”必须声明一个主体,因为它没有标记为抽象或外部

任何有关我做错的信息将不胜感激。

I have this POGO ( simple putter getter) class that I am trying to read in PowerShell

using System;
using System.Runtime.Serialization;

namespace MyApp.VM
{
  [Serializable]
  public class MyClassVM
  {
      public Int64 CtrId { get; set; }
      public string CtrName { get; set; }
      public string CtrPhone { get; set; }
      public string CtrZip { get; set; }
      public DateTime AddDate { get; set; }
  }
}

Here is the ps1 code that is trying to read the class from a file.

function Build-Pogo
{
    $FileDir   = "D:\YourDirectoryOfPogo" 
    $ClassName = "MyClassVM"
    $FileName  = $FileDir + "\" + $ClassName + ".cs"

    # Build the class from the file
    $AllLines = [string]::join([environment]::newline, (Get-Content $FileName))
    Add-Type -TypeDefinition $AllLines

    # spin thru each property for class
    $ClassHandle = New-Object -typeName $ClassName
    $ClassHandle | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}
}

*Note the last line is placeholder for more complex logic to come later.

This breaks at the Add-Type with this error message for each get/set in the file.

'MyApp.VM.MyClassVM.CtrId.get' must declare a body because it is not marked abstract or extern

Any info on what I'm doing wrong will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萤火眠眠 2024-10-20 08:25:35

试试这个代码,它对我有用。

$type = Add-Type -Path $FileName -PassThru

$x = New-Object $type
$x.CtrId = 500
$x.CtrName = 'Testing'
$x.CtrPhone = '555-1212'
$x.CtrZip = '12345'
$x.AddDate = Get-Date

$x

输出:

CtrId    : 500
CtrName  : Testing
CtrPhone : 555-1212
CtrZip   : 12345
AddDate  : 1/28/2011 6:16:26 PM

Try this code, it worked for me.

$type = Add-Type -Path $FileName -PassThru

$x = New-Object $type
$x.CtrId = 500
$x.CtrName = 'Testing'
$x.CtrPhone = '555-1212'
$x.CtrZip = '12345'
$x.AddDate = Get-Date

$x

Output:

CtrId    : 500
CtrName  : Testing
CtrPhone : 555-1212
CtrZip   : 12345
AddDate  : 1/28/2011 6:16:26 PM
陌上青苔 2024-10-20 08:25:35

由于您在类型定义中使用属性快捷方式,因此需要确保通过在 Add-Type 命令中使用 -Language CSharpVersion3 使用 C# v3 进行编译。

正如 @voodoomsr 指出的,您必须为 New-Object 提供命名空间,或者您可以从 Add-Type 返回类型,就像 @Chuck 对 - 所做的那样PassThru 参数。

以下是 Build-POGO 函数的示例:

function Build-Pogo
{
    $FileDir   = "D:\YourDirectoryOfPogo" 
    $ClassName = "MyClassVM"
    $FileName  = $FileDir + "\" + $ClassName + ".cs"

    $AllLines = (Get-Content $FileName) -join "`n"
    $type = Add-Type -TypeDefinition $AllLines -Language CSharpVersion3 -PassThru

    New-Object $type
}

Since you are using property shortcuts in your type definition, you need to make sure to compile using C# v3 by using -Language CSharpVersion3 in the Add-Type command.

As @voodoomsr pointed out, you must supply the Namespace for New-Object, or you can return the type from Add-Type as @Chuck did with the -PassThru parameter.

Here is an example of the Build-POGO function:

function Build-Pogo
{
    $FileDir   = "D:\YourDirectoryOfPogo" 
    $ClassName = "MyClassVM"
    $FileName  = $FileDir + "\" + $ClassName + ".cs"

    $AllLines = (Get-Content $FileName) -join "`n"
    $type = Add-Type -TypeDefinition $AllLines -Language CSharpVersion3 -PassThru

    New-Object $type
}
娇纵 2024-10-20 08:25:35

您有 2 个错误,1:缺少类型的命名空间,2:您没有打印任何内容。
我给你一个可能的更正:

$ClassHandle = New-Object -typeName MyApp.VM.$ClassName
$ClassHandle | fl     #basic way to print the members

更漂亮的成员打印(属性)

 $ClassHandle | gm -MemberType Property |
 % {write-host $_.name  -for red -nonewline; 
 [console]::setcursorposition(15,[console]::cursortop);
 write-host $classhandle.($_.name) -f white}

You have 2 errors, 1: the missing Namespace of the type, 2: you are not printing anything.
I give you a possible correction:

$ClassHandle = New-Object -typeName MyApp.VM.$ClassName
$ClassHandle | fl     #basic way to print the members

a more beautiful print of the members(Properties)

 $ClassHandle | gm -MemberType Property |
 % {write-host $_.name  -for red -nonewline; 
 [console]::setcursorposition(15,[console]::cursortop);
 write-host $classhandle.($_.name) -f white}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文