如何使用 Powershell 从另一个类 (.cs) 文件读取来创建类?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个代码,它对我有用。
输出:
Try this code, it worked for me.
Output:
由于您在类型定义中使用属性快捷方式,因此需要确保通过在
Add-Type
命令中使用-Language CSharpVersion3
使用 C# v3 进行编译。正如 @voodoomsr 指出的,您必须为
New-Object
提供命名空间,或者您可以从Add-Type
返回类型,就像 @Chuck 对- 所做的那样PassThru
参数。以下是
Build-POGO
函数的示例: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 theAdd-Type
command.As @voodoomsr pointed out, you must supply the Namespace for
New-Object
, or you can return the type fromAdd-Type
as @Chuck did with the-PassThru
parameter.Here is an example of the
Build-POGO
function:您有 2 个错误,1:缺少类型的命名空间,2:您没有打印任何内容。
我给你一个可能的更正:
更漂亮的成员打印(属性)
You have 2 errors, 1: the missing Namespace of the type, 2: you are not printing anything.
I give you a possible correction:
a more beautiful print of the members(Properties)