C# 如何在PowerShell中的用户定义类中使用用户定义的结构?

C# 如何在PowerShell中的用户定义类中使用用户定义的结构?,c#,class,powershell,struct,add-type,C#,Class,Powershell,Struct,Add Type,我在PowerShell 3.0和4.0上试过这个。我在类中使用结构时遇到问题。我可以直接从PowerShell使用结构内部的属性,而不会产生任何问题。我还可以直接从PowerShell使用类内的任何标准类型属性,而不会产生任何问题。但是,当我将这两者结合起来尝试在类中使用自定义类型属性时,我不能 任何帮助都将不胜感激 下面是一个快速的示例代码,用于重现我看到的内容: $MyTest = Add-Type @" namespace MyTest { public struct Struc

我在PowerShell 3.0和4.0上试过这个。我在类中使用结构时遇到问题。我可以直接从PowerShell使用结构内部的属性,而不会产生任何问题。我还可以直接从PowerShell使用类内的任何标准类型属性,而不会产生任何问题。但是,当我将这两者结合起来尝试在类中使用自定义类型属性时,我不能

任何帮助都将不胜感激

下面是一个快速的示例代码,用于重现我看到的内容:

$MyTest = Add-Type @"
namespace MyTest
{
    public struct Struct1
    {
        public string Property;
    }

    public class Class1
    {
        public struct Struct2
        {
            public string Property;
        }

        public string MyString;
        public Struct1 Struct1Property;
        public Struct2 Struct2Property;
    }
}
"@ -PassThru

$struct1 = New-Object -TypeName MyTest.Struct1
$class1 = New-Object -TypeName MyTest.Class1
$struct1.Property = 'test'
$struct1.Property   # Outputs: test
$class1.MyString = 'test'
$class1.MyString    # Outputs: test
$class1.Struct1Property.Property = 'test'
$class1.Struct1Property.Property    # Outputs: <nothing>
$class1.Struct2Property.Property = 'test'
$class1.Struct2Property.Property    # Outputs: <nothing>
输出:

struct1.Property: test
class1.Struct1Property.Property: test
class1.Struct2Property.Property: test

Technet上已经回答了这个问题:

这些字段不是属性,这有很大区别。如果它们是属性,那么等价的C代码甚至不会编译。你指的是我的“属性选择”一词吗?我从Get-Member cmdlet的MemberType输出中窃取了它。即使这个词本身是武断的,我为任何混淆道歉。你是对的,它们确实是字段。无论如何,PowerShell似乎不像C那样处理结构。
struct1.Property: test
class1.Struct1Property.Property: test
class1.Struct2Property.Property: test