Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自PowerShell对象的C#对象_C# - Fatal编程技术网

来自PowerShell对象的C#对象

来自PowerShell对象的C#对象,c#,C#,我目前正在从事一个PowerShell项目,并且一直在使用PowerShell v5类为输出对象创建自定义类型。然而,由于这些类的当前实现中存在各种失败,我决定尝试用C#创建这些类 在创建类构造函数时,我遇到了一个无法完全理解的问题。我正在使用PowerShell的Invoke RestMethod,它返回PSCustomObject类型的对象。我需要使用这些对象的属性来实例化C#类中的对象,并计算一些附加属性。PowerShell构造函数示例如下所示: class user { [st

我目前正在从事一个PowerShell项目,并且一直在使用PowerShell v5类为输出对象创建自定义类型。然而,由于这些类的当前实现中存在各种失败,我决定尝试用C#创建这些类

在创建类构造函数时,我遇到了一个无法完全理解的问题。我正在使用PowerShell的Invoke RestMethod,它返回PSCustomObject类型的对象。我需要使用这些对象的属性来实例化C#类中的对象,并计算一些附加属性。PowerShell构造函数示例如下所示:

class user
{
    [string]
    $username

    [string]
    $userprincipalname

    [hashtable]
    $custom_attributes

    static [string] getStatus ([int]$Status)
    {
        { return [UserStatus]([int]$Status)
    }

    static [hashtable] getCustom_Attributes ([PSObject]$custom_attributes)
    {
        $Output = @{}
        $custom_attributes.PSObject.Properties | ForEach-Object {
            if ($_.Value) {$Output[$_.Name] = $_.Value}
        }
        return $Output
    }

    User ([PSCustomObject]$InputObject)
    {
        $this.username          = $InputObject.username
        $this.userprincipalname = $InputObject.userprincipalname
        $this.custom_attributes = [User]::getCustom_Attributes($InputObject.custom_attributes)
    }
}
现在,我在C#中尝试了类似的方法,但似乎无法在构造函数中引用构造函数参数属性:

using System;
using System.Management.Automation;
namespace test
{    
    public class user 
    {
        public string username {get; set;}
        public string userprincipalname {get; set;}
        public object[] custom_attributes {get; set;}

        static string getStatus (int Status)
        {
            return Enum.GetName(typeof(test.userstatus), Status);
        }

        user (PSCustomObject InputObject)
        {
            this.username          = InputObject.username;
            this.userprincipalname = InputObject.userprincipalname;
            this.custom_attribute  = test.User.getCustom_Attributes(InputObject.custom_attributes);
        }
    }

    public enum userstatus
    {
        Active,
        Inactive
    }
}
使用“添加类型”导入此文件时,出现错误:

“System.Management.Automation.PSCustomObject”不包含“username”的定义,并且找不到接受“System.Management.Automation.PSCustomObject”类型的第一个参数的扩展方法“username”(是否缺少using指令或assemblyreference?)

我是不是完全错了?如何从PSCUstomObjects创建C#对象,同时还能计算一些附加或替换属性?
谢谢

我找到了一个更简单的解决方案,我相信它能满足我的需要。我不依赖构造函数,而是使用自定义getter来创建我的计算属性:

public class User
{
    public string username {get; set;}
    public string userprincipalname {get; set;}
    public string status {get; set;}
    public string status_value
    {
        get
        {
            return Enum.GetName(typeof(UserStatus), Int32.Parse(this.status));
        } 
    }
}
到目前为止,这似乎是可行的,我已经完全放弃了我的构造函数,只使用默认构造函数。我相信我仍然可以使用构造函数来处理这些问题,并从getter得到相同的结果


谢谢大家的帮助

这是实际代码吗?看看你的口袋里有什么constructor@JeroenVannevel谢谢,我明白你的意思了。这与我正在使用的实际代码类似,但我的类还有几个其他属性。看起来我做了一个错误的复制/粘贴,并在构造函数中包含了错误的属性。这是固定在上面的职位现在。然而,这并不是我真正遇到的问题,因为实际的类具有正确的属性名。C#和PowerShell是非常不同的语言。简而言之,C#不是这样工作的,PowerShell的类更多的是事后思考。与PowerShell处理对象的功能相当的C#是
ExpandoObject
dynamic
,但这可能不是您想要的。将
哈希表
传递给构造函数更符合逻辑。@Jeroenmoster我同意-我发现PowerShell类可以满足许多需求,但它们的开发程度还不够,无法始终如一地工作。这就是为什么我试图在这个项目中使用C#类。很多人已经这样做了一段时间,但正如你所看到的,我仍然是一个完全的语言初学者。我意识到C#不是这样工作的,但还没有找到一个我想要实现的例子。你知道有一个关于哈希表的例子吗?谢谢我知道我可以用构造函数参数完成所有这些,但是我的一些类会有15个、20个或更多属性,对象实例化看起来很糟糕。